blob: e50442aa0b66085bee4775b187323b8fef16e654 [file] [log] [blame]
Shrenuj Bansala419c792016-10-20 14:05:11 -07001/* Copyright (c) 2008-2017, The Linux Foundation. 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/module.h>
14#include <linux/fb.h>
15#include <linux/file.h>
16#include <linux/fs.h>
17#include <linux/fdtable.h>
18#include <linux/list.h>
19#include <linux/debugfs.h>
20#include <linux/uaccess.h>
21#include <linux/interrupt.h>
22#include <linux/workqueue.h>
23#include <linux/dma-buf.h>
24#include <linux/pm_runtime.h>
25#include <linux/rbtree.h>
26#include <linux/major.h>
27#include <linux/io.h>
28#include <linux/mman.h>
29#include <linux/sort.h>
30#include <linux/security.h>
31#include <linux/compat.h>
32#include <linux/ctype.h>
33#include <linux/mm.h>
34#include <asm/cacheflush.h>
35
36#include "kgsl.h"
37#include "kgsl_debugfs.h"
38#include "kgsl_log.h"
39#include "kgsl_sharedmem.h"
40#include "kgsl_drawobj.h"
41#include "kgsl_device.h"
42#include "kgsl_trace.h"
43#include "kgsl_sync.h"
44#include "kgsl_compat.h"
45#include "kgsl_pool.h"
46
47#undef MODULE_PARAM_PREFIX
48#define MODULE_PARAM_PREFIX "kgsl."
49
50#ifndef arch_mmap_check
51#define arch_mmap_check(addr, len, flags) (0)
52#endif
53
54#ifndef pgprot_writebackcache
55#define pgprot_writebackcache(_prot) (_prot)
56#endif
57
58#ifndef pgprot_writethroughcache
59#define pgprot_writethroughcache(_prot) (_prot)
60#endif
61
62#ifdef CONFIG_ARM_LPAE
63#define KGSL_DMA_BIT_MASK DMA_BIT_MASK(64)
64#else
65#define KGSL_DMA_BIT_MASK DMA_BIT_MASK(32)
66#endif
67
68static char *kgsl_mmu_type;
69module_param_named(mmutype, kgsl_mmu_type, charp, 0000);
70MODULE_PARM_DESC(kgsl_mmu_type, "Type of MMU to be used for graphics");
71
72/* Mutex used for the IOMMU sync quirk */
73DEFINE_MUTEX(kgsl_mmu_sync);
74EXPORT_SYMBOL(kgsl_mmu_sync);
75
76struct kgsl_dma_buf_meta {
77 struct dma_buf_attachment *attach;
78 struct dma_buf *dmabuf;
79 struct sg_table *table;
80};
81
82static inline struct kgsl_pagetable *_get_memdesc_pagetable(
83 struct kgsl_pagetable *pt, struct kgsl_mem_entry *entry)
84{
85 /* if a secured buffer, map it to secure global pagetable */
86 if (kgsl_memdesc_is_secured(&entry->memdesc))
87 return pt->mmu->securepagetable;
88
89 return pt;
90}
91
92static void kgsl_mem_entry_detach_process(struct kgsl_mem_entry *entry);
93
94static const struct file_operations kgsl_fops;
95
96/*
97 * The memfree list contains the last N blocks of memory that have been freed.
98 * On a GPU fault we walk the list to see if the faulting address had been
99 * recently freed and print out a message to that effect
100 */
101
102#define MEMFREE_ENTRIES 512
103
104static DEFINE_SPINLOCK(memfree_lock);
105
106struct memfree_entry {
107 pid_t ptname;
108 uint64_t gpuaddr;
109 uint64_t size;
110 pid_t pid;
111 uint64_t flags;
112};
113
114static struct {
115 struct memfree_entry *list;
116 int head;
117 int tail;
118} memfree;
119
120static int kgsl_memfree_init(void)
121{
122 memfree.list = kcalloc(MEMFREE_ENTRIES, sizeof(struct memfree_entry),
123 GFP_KERNEL);
124
125 return (memfree.list) ? 0 : -ENOMEM;
126}
127
128static void kgsl_memfree_exit(void)
129{
130 kfree(memfree.list);
131 memset(&memfree, 0, sizeof(memfree));
132}
133
134static inline bool match_memfree_addr(struct memfree_entry *entry,
135 pid_t ptname, uint64_t gpuaddr)
136{
137 return ((entry->ptname == ptname) &&
138 (entry->size > 0) &&
139 (gpuaddr >= entry->gpuaddr &&
140 gpuaddr < (entry->gpuaddr + entry->size)));
141}
142int kgsl_memfree_find_entry(pid_t ptname, uint64_t *gpuaddr,
143 uint64_t *size, uint64_t *flags, pid_t *pid)
144{
145 int ptr;
146
147 if (memfree.list == NULL)
148 return 0;
149
150 spin_lock(&memfree_lock);
151
152 ptr = memfree.head - 1;
153 if (ptr < 0)
154 ptr = MEMFREE_ENTRIES - 1;
155
156 /* Walk backwards through the list looking for the last match */
157 while (ptr != memfree.tail) {
158 struct memfree_entry *entry = &memfree.list[ptr];
159
160 if (match_memfree_addr(entry, ptname, *gpuaddr)) {
161 *gpuaddr = entry->gpuaddr;
162 *flags = entry->flags;
163 *size = entry->size;
164 *pid = entry->pid;
165
166 spin_unlock(&memfree_lock);
167 return 1;
168 }
169
170 ptr = ptr - 1;
171
172 if (ptr < 0)
173 ptr = MEMFREE_ENTRIES - 1;
174 }
175
176 spin_unlock(&memfree_lock);
177 return 0;
178}
179
180static void kgsl_memfree_purge(struct kgsl_pagetable *pagetable,
181 uint64_t gpuaddr, uint64_t size)
182{
183 pid_t ptname = pagetable ? pagetable->name : 0;
184 int i;
185
186 if (memfree.list == NULL)
187 return;
188
189 spin_lock(&memfree_lock);
190
191 for (i = 0; i < MEMFREE_ENTRIES; i++) {
192 struct memfree_entry *entry = &memfree.list[i];
193
194 if (entry->ptname != ptname || entry->size == 0)
195 continue;
196
197 if (gpuaddr > entry->gpuaddr &&
198 gpuaddr < entry->gpuaddr + entry->size) {
199 /* truncate the end of the entry */
200 entry->size = gpuaddr - entry->gpuaddr;
201 } else if (gpuaddr <= entry->gpuaddr) {
202 if (gpuaddr + size > entry->gpuaddr &&
203 gpuaddr + size < entry->gpuaddr + entry->size)
204 /* Truncate the beginning of the entry */
205 entry->gpuaddr = gpuaddr + size;
206 else if (gpuaddr + size >= entry->gpuaddr + entry->size)
207 /* Remove the entire entry */
208 entry->size = 0;
209 }
210 }
211 spin_unlock(&memfree_lock);
212}
213
214static void kgsl_memfree_add(pid_t pid, pid_t ptname, uint64_t gpuaddr,
215 uint64_t size, uint64_t flags)
216
217{
218 struct memfree_entry *entry;
219
220 if (memfree.list == NULL)
221 return;
222
223 spin_lock(&memfree_lock);
224
225 entry = &memfree.list[memfree.head];
226
227 entry->pid = pid;
228 entry->ptname = ptname;
229 entry->gpuaddr = gpuaddr;
230 entry->size = size;
231 entry->flags = flags;
232
233 memfree.head = (memfree.head + 1) % MEMFREE_ENTRIES;
234
235 if (memfree.head == memfree.tail)
236 memfree.tail = (memfree.tail + 1) % MEMFREE_ENTRIES;
237
238 spin_unlock(&memfree_lock);
239}
240
241int kgsl_readtimestamp(struct kgsl_device *device, void *priv,
242 enum kgsl_timestamp_type type, unsigned int *timestamp)
243{
244 return device->ftbl->readtimestamp(device, priv, type, timestamp);
245}
246EXPORT_SYMBOL(kgsl_readtimestamp);
247
248static long gpumem_free_entry(struct kgsl_mem_entry *entry);
249
250/* Scheduled by kgsl_mem_entry_put_deferred() */
251static void _deferred_put(struct work_struct *work)
252{
253 struct kgsl_mem_entry *entry =
254 container_of(work, struct kgsl_mem_entry, work);
255
256 kgsl_mem_entry_put(entry);
257}
258
259static inline struct kgsl_mem_entry *
260kgsl_mem_entry_create(void)
261{
262 struct kgsl_mem_entry *entry = kzalloc(sizeof(*entry), GFP_KERNEL);
263
264 if (entry != NULL)
265 kref_init(&entry->refcount);
266
267 return entry;
268}
269#ifdef CONFIG_DMA_SHARED_BUFFER
270static void kgsl_destroy_ion(struct kgsl_dma_buf_meta *meta)
271{
272 if (meta != NULL) {
273 dma_buf_unmap_attachment(meta->attach, meta->table,
274 DMA_FROM_DEVICE);
275 dma_buf_detach(meta->dmabuf, meta->attach);
276 dma_buf_put(meta->dmabuf);
277 kfree(meta);
278 }
279}
280#else
281static void kgsl_destroy_ion(struct kgsl_dma_buf_meta *meta)
282{
283
284}
285#endif
286
287void
288kgsl_mem_entry_destroy(struct kref *kref)
289{
290 struct kgsl_mem_entry *entry = container_of(kref,
291 struct kgsl_mem_entry,
292 refcount);
293 unsigned int memtype;
294
295 if (entry == NULL)
296 return;
297
298 /* pull out the memtype before the flags get cleared */
299 memtype = kgsl_memdesc_usermem_type(&entry->memdesc);
300
301 /* Detach from process list */
302 kgsl_mem_entry_detach_process(entry);
303
304 if (memtype != KGSL_MEM_ENTRY_KERNEL)
305 atomic_long_sub(entry->memdesc.size,
306 &kgsl_driver.stats.mapped);
307
308 /*
309 * Ion takes care of freeing the sg_table for us so
310 * clear the sg table before freeing the sharedmem
311 * so kgsl_sharedmem_free doesn't try to free it again
312 */
313 if (memtype == KGSL_MEM_ENTRY_ION)
314 entry->memdesc.sgt = NULL;
315
316 if ((memtype == KGSL_MEM_ENTRY_USER)
317 && !(entry->memdesc.flags & KGSL_MEMFLAGS_GPUREADONLY)) {
318 int i = 0, j;
319 struct scatterlist *sg;
320 struct page *page;
321 /*
322 * Mark all of pages in the scatterlist as dirty since they
323 * were writable by the GPU.
324 */
325 for_each_sg(entry->memdesc.sgt->sgl, sg,
326 entry->memdesc.sgt->nents, i) {
327 page = sg_page(sg);
328 for (j = 0; j < (sg->length >> PAGE_SHIFT); j++)
329 set_page_dirty(nth_page(page, j));
330 }
331 }
332
333 kgsl_sharedmem_free(&entry->memdesc);
334
335 switch (memtype) {
336 case KGSL_MEM_ENTRY_ION:
337 kgsl_destroy_ion(entry->priv_data);
338 break;
339 default:
340 break;
341 }
342
343 kfree(entry);
344}
345EXPORT_SYMBOL(kgsl_mem_entry_destroy);
346
347/* Allocate a IOVA for memory objects that don't use SVM */
348static int kgsl_mem_entry_track_gpuaddr(struct kgsl_device *device,
349 struct kgsl_process_private *process,
350 struct kgsl_mem_entry *entry)
351{
352 struct kgsl_pagetable *pagetable;
353
354 /*
355 * If SVM is enabled for this object then the address needs to be
356 * assigned elsewhere
357 * Also do not proceed further in case of NoMMU.
358 */
359 if (kgsl_memdesc_use_cpu_map(&entry->memdesc) ||
360 (kgsl_mmu_get_mmutype(device) == KGSL_MMU_TYPE_NONE))
361 return 0;
362
363 pagetable = kgsl_memdesc_is_secured(&entry->memdesc) ?
364 device->mmu.securepagetable : process->pagetable;
365
366 return kgsl_mmu_get_gpuaddr(pagetable, &entry->memdesc);
367}
368
369/* Commit the entry to the process so it can be accessed by other operations */
370static void kgsl_mem_entry_commit_process(struct kgsl_mem_entry *entry)
371{
372 if (!entry)
373 return;
374
375 spin_lock(&entry->priv->mem_lock);
376 idr_replace(&entry->priv->mem_idr, entry, entry->id);
377 spin_unlock(&entry->priv->mem_lock);
378}
379
380/*
381 * Attach the memory object to a process by (possibly) getting a GPU address and
382 * (possibly) mapping it
383 */
384static int kgsl_mem_entry_attach_process(struct kgsl_device *device,
385 struct kgsl_process_private *process,
386 struct kgsl_mem_entry *entry)
387{
388 int id, ret;
389
390 ret = kgsl_process_private_get(process);
391 if (!ret)
392 return -EBADF;
393
394 ret = kgsl_mem_entry_track_gpuaddr(device, process, entry);
395 if (ret) {
396 kgsl_process_private_put(process);
397 return ret;
398 }
399
400 idr_preload(GFP_KERNEL);
401 spin_lock(&process->mem_lock);
402 /* Allocate the ID but don't attach the pointer just yet */
403 id = idr_alloc(&process->mem_idr, NULL, 1, 0, GFP_NOWAIT);
404 spin_unlock(&process->mem_lock);
405 idr_preload_end();
406
407 if (id < 0) {
408 if (!kgsl_memdesc_use_cpu_map(&entry->memdesc))
409 kgsl_mmu_put_gpuaddr(&entry->memdesc);
410 kgsl_process_private_put(process);
411 return id;
412 }
413
414 entry->id = id;
415 entry->priv = process;
416
417 /*
418 * Map the memory if a GPU address is already assigned, either through
419 * kgsl_mem_entry_track_gpuaddr() or via some other SVM process
420 */
421 if (entry->memdesc.gpuaddr) {
422 if (entry->memdesc.flags & KGSL_MEMFLAGS_SPARSE_VIRT)
423 ret = kgsl_mmu_sparse_dummy_map(
424 entry->memdesc.pagetable,
425 &entry->memdesc, 0,
426 entry->memdesc.size);
427 else if (entry->memdesc.gpuaddr)
428 ret = kgsl_mmu_map(entry->memdesc.pagetable,
429 &entry->memdesc);
430
431 if (ret)
432 kgsl_mem_entry_detach_process(entry);
433 }
434
435 kgsl_memfree_purge(entry->memdesc.pagetable, entry->memdesc.gpuaddr,
436 entry->memdesc.size);
437
438 return ret;
439}
440
441/* Detach a memory entry from a process and unmap it from the MMU */
442static void kgsl_mem_entry_detach_process(struct kgsl_mem_entry *entry)
443{
444 unsigned int type;
445
446 if (entry == NULL)
447 return;
448
449 /*
450 * First remove the entry from mem_idr list
451 * so that no one can operate on obsolete values
452 */
453 spin_lock(&entry->priv->mem_lock);
454 if (entry->id != 0)
455 idr_remove(&entry->priv->mem_idr, entry->id);
456 entry->id = 0;
457
458 type = kgsl_memdesc_usermem_type(&entry->memdesc);
459 entry->priv->stats[type].cur -= entry->memdesc.size;
460 spin_unlock(&entry->priv->mem_lock);
461
462 kgsl_mmu_put_gpuaddr(&entry->memdesc);
463
464 kgsl_process_private_put(entry->priv);
465
466 entry->priv = NULL;
467}
468
469/**
470 * kgsl_context_dump() - dump information about a draw context
471 * @device: KGSL device that owns the context
472 * @context: KGSL context to dump information about
473 *
474 * Dump specific information about the context to the kernel log. Used for
475 * fence timeout callbacks
476 */
477void kgsl_context_dump(struct kgsl_context *context)
478{
479 struct kgsl_device *device;
480
481 if (_kgsl_context_get(context) == 0)
482 return;
483
484 device = context->device;
485
486 if (kgsl_context_detached(context)) {
487 dev_err(device->dev, " context[%d]: context detached\n",
488 context->id);
489 } else if (device->ftbl->drawctxt_dump != NULL)
490 device->ftbl->drawctxt_dump(device, context);
491
492 kgsl_context_put(context);
493}
494EXPORT_SYMBOL(kgsl_context_dump);
495
496/* Allocate a new context ID */
497static int _kgsl_get_context_id(struct kgsl_device *device)
498{
499 int id;
500
501 idr_preload(GFP_KERNEL);
502 write_lock(&device->context_lock);
503 /* Allocate the slot but don't put a pointer in it yet */
504 id = idr_alloc(&device->context_idr, NULL, 1,
505 KGSL_MEMSTORE_MAX, GFP_NOWAIT);
506 write_unlock(&device->context_lock);
507 idr_preload_end();
508
509 return id;
510}
511
512/**
513 * kgsl_context_init() - helper to initialize kgsl_context members
514 * @dev_priv: the owner of the context
515 * @context: the newly created context struct, should be allocated by
516 * the device specific drawctxt_create function.
517 *
518 * This is a helper function for the device specific drawctxt_create
519 * function to initialize the common members of its context struct.
520 * If this function succeeds, reference counting is active in the context
521 * struct and the caller should kgsl_context_put() it on error.
522 * If it fails, the caller should just free the context structure
523 * it passed in.
524 */
525int kgsl_context_init(struct kgsl_device_private *dev_priv,
526 struct kgsl_context *context)
527{
528 struct kgsl_device *device = dev_priv->device;
529 char name[64];
530 int ret = 0, id;
531
532 id = _kgsl_get_context_id(device);
533 if (id == -ENOSPC) {
534 /*
535 * Before declaring that there are no contexts left try
536 * flushing the event workqueue just in case there are
537 * detached contexts waiting to finish
538 */
539
540 flush_workqueue(device->events_wq);
541 id = _kgsl_get_context_id(device);
542 }
543
544 if (id < 0) {
545 if (id == -ENOSPC)
546 KGSL_DRV_INFO(device,
547 "cannot have more than %zu contexts due to memstore limitation\n",
548 KGSL_MEMSTORE_MAX);
549
550 return id;
551 }
552
553 context->id = id;
554
555 kref_init(&context->refcount);
556 /*
557 * Get a refernce to the process private so its not destroyed, until
558 * the context is destroyed. This will also prevent the pagetable
559 * from being destroyed
560 */
561 if (!kgsl_process_private_get(dev_priv->process_priv)) {
562 ret = -EBADF;
563 goto out;
564 }
565 context->device = dev_priv->device;
566 context->dev_priv = dev_priv;
567 context->proc_priv = dev_priv->process_priv;
568 context->tid = task_pid_nr(current);
569
570 ret = kgsl_sync_timeline_create(context);
571 if (ret)
572 goto out;
573
574 snprintf(name, sizeof(name), "context-%d", id);
575 kgsl_add_event_group(&context->events, context, name,
576 kgsl_readtimestamp, context);
577
578out:
579 if (ret) {
580 write_lock(&device->context_lock);
581 idr_remove(&dev_priv->device->context_idr, id);
582 write_unlock(&device->context_lock);
583 }
584
585 return ret;
586}
587EXPORT_SYMBOL(kgsl_context_init);
588
589/**
590 * kgsl_context_detach() - Release the "master" context reference
591 * @context: The context that will be detached
592 *
593 * This is called when a context becomes unusable, because userspace
594 * has requested for it to be destroyed. The context itself may
595 * exist a bit longer until its reference count goes to zero.
596 * Other code referencing the context can detect that it has been
597 * detached by checking the KGSL_CONTEXT_PRIV_DETACHED bit in
598 * context->priv.
599 */
600static void kgsl_context_detach(struct kgsl_context *context)
601{
602 struct kgsl_device *device;
603
604 if (context == NULL)
605 return;
606
607 /*
608 * Mark the context as detached to keep others from using
609 * the context before it gets fully removed, and to make sure
610 * we don't try to detach twice.
611 */
612 if (test_and_set_bit(KGSL_CONTEXT_PRIV_DETACHED, &context->priv))
613 return;
614
615 device = context->device;
616
617 trace_kgsl_context_detach(device, context);
618
619 context->device->ftbl->drawctxt_detach(context);
620
621 /*
622 * Cancel all pending events after the device-specific context is
623 * detached, to avoid possibly freeing memory while it is still
624 * in use by the GPU.
625 */
626 kgsl_cancel_events(device, &context->events);
627
628 /* Remove the event group from the list */
629 kgsl_del_event_group(&context->events);
630
631 kgsl_context_put(context);
632}
633
634void
635kgsl_context_destroy(struct kref *kref)
636{
637 struct kgsl_context *context = container_of(kref, struct kgsl_context,
638 refcount);
639 struct kgsl_device *device = context->device;
640
641 trace_kgsl_context_destroy(device, context);
642
643 /*
644 * It's not safe to destroy the context if it's not detached as GPU
645 * may still be executing commands
646 */
647 BUG_ON(!kgsl_context_detached(context));
648
649 write_lock(&device->context_lock);
650 if (context->id != KGSL_CONTEXT_INVALID) {
651
652 /* Clear the timestamps in the memstore during destroy */
653 kgsl_sharedmem_writel(device, &device->memstore,
654 KGSL_MEMSTORE_OFFSET(context->id, soptimestamp), 0);
655 kgsl_sharedmem_writel(device, &device->memstore,
656 KGSL_MEMSTORE_OFFSET(context->id, eoptimestamp), 0);
657
658 /* clear device power constraint */
659 if (context->id == device->pwrctrl.constraint.owner_id) {
660 trace_kgsl_constraint(device,
661 device->pwrctrl.constraint.type,
662 device->pwrctrl.active_pwrlevel,
663 0);
664 device->pwrctrl.constraint.type = KGSL_CONSTRAINT_NONE;
665 }
666
667 idr_remove(&device->context_idr, context->id);
668 context->id = KGSL_CONTEXT_INVALID;
669 }
670 write_unlock(&device->context_lock);
671 kgsl_sync_timeline_destroy(context);
672 kgsl_process_private_put(context->proc_priv);
673
674 device->ftbl->drawctxt_destroy(context);
675}
676
677struct kgsl_device *kgsl_get_device(int dev_idx)
678{
679 int i;
680 struct kgsl_device *ret = NULL;
681
682 mutex_lock(&kgsl_driver.devlock);
683
684 for (i = 0; i < KGSL_DEVICE_MAX; i++) {
685 if (kgsl_driver.devp[i] && kgsl_driver.devp[i]->id == dev_idx) {
686 ret = kgsl_driver.devp[i];
687 break;
688 }
689 }
690
691 mutex_unlock(&kgsl_driver.devlock);
692 return ret;
693}
694EXPORT_SYMBOL(kgsl_get_device);
695
696static struct kgsl_device *kgsl_get_minor(int minor)
697{
698 struct kgsl_device *ret = NULL;
699
700 if (minor < 0 || minor >= KGSL_DEVICE_MAX)
701 return NULL;
702
703 mutex_lock(&kgsl_driver.devlock);
704 ret = kgsl_driver.devp[minor];
705 mutex_unlock(&kgsl_driver.devlock);
706
707 return ret;
708}
709
710/**
711 * kgsl_check_timestamp() - return true if the specified timestamp is retired
712 * @device: Pointer to the KGSL device to check
713 * @context: Pointer to the context for the timestamp
714 * @timestamp: The timestamp to compare
715 */
716int kgsl_check_timestamp(struct kgsl_device *device,
717 struct kgsl_context *context, unsigned int timestamp)
718{
719 unsigned int ts_processed;
720
721 kgsl_readtimestamp(device, context, KGSL_TIMESTAMP_RETIRED,
722 &ts_processed);
723
724 return (timestamp_cmp(ts_processed, timestamp) >= 0);
725}
726EXPORT_SYMBOL(kgsl_check_timestamp);
727
728static int kgsl_suspend_device(struct kgsl_device *device, pm_message_t state)
729{
730 int status = -EINVAL;
731
732 if (!device)
733 return -EINVAL;
734
735 KGSL_PWR_WARN(device, "suspend start\n");
736
737 mutex_lock(&device->mutex);
738 status = kgsl_pwrctrl_change_state(device, KGSL_STATE_SUSPEND);
739 mutex_unlock(&device->mutex);
740
741 KGSL_PWR_WARN(device, "suspend end\n");
742 return status;
743}
744
745static int kgsl_resume_device(struct kgsl_device *device)
746{
747 if (!device)
748 return -EINVAL;
749
750 KGSL_PWR_WARN(device, "resume start\n");
751 mutex_lock(&device->mutex);
752 if (device->state == KGSL_STATE_SUSPEND) {
753 kgsl_pwrctrl_change_state(device, KGSL_STATE_SLUMBER);
754 } else if (device->state != KGSL_STATE_INIT) {
755 /*
756 * This is an error situation,so wait for the device
757 * to idle and then put the device to SLUMBER state.
758 * This will put the device to the right state when
759 * we resume.
760 */
761 if (device->state == KGSL_STATE_ACTIVE)
762 device->ftbl->idle(device);
763 kgsl_pwrctrl_change_state(device, KGSL_STATE_SLUMBER);
764 KGSL_PWR_ERR(device,
765 "resume invoked without a suspend\n");
766 }
767
768 mutex_unlock(&device->mutex);
769 KGSL_PWR_WARN(device, "resume end\n");
770 return 0;
771}
772
773static int kgsl_suspend(struct device *dev)
774{
775
776 pm_message_t arg = {0};
777 struct kgsl_device *device = dev_get_drvdata(dev);
778
779 return kgsl_suspend_device(device, arg);
780}
781
782static int kgsl_resume(struct device *dev)
783{
784 struct kgsl_device *device = dev_get_drvdata(dev);
785
786 return kgsl_resume_device(device);
787}
788
789static int kgsl_runtime_suspend(struct device *dev)
790{
791 return 0;
792}
793
794static int kgsl_runtime_resume(struct device *dev)
795{
796 return 0;
797}
798
799const struct dev_pm_ops kgsl_pm_ops = {
800 .suspend = kgsl_suspend,
801 .resume = kgsl_resume,
802 .runtime_suspend = kgsl_runtime_suspend,
803 .runtime_resume = kgsl_runtime_resume,
804};
805EXPORT_SYMBOL(kgsl_pm_ops);
806
807int kgsl_suspend_driver(struct platform_device *pdev,
808 pm_message_t state)
809{
810 struct kgsl_device *device = dev_get_drvdata(&pdev->dev);
811
812 return kgsl_suspend_device(device, state);
813}
814EXPORT_SYMBOL(kgsl_suspend_driver);
815
816int kgsl_resume_driver(struct platform_device *pdev)
817{
818 struct kgsl_device *device = dev_get_drvdata(&pdev->dev);
819
820 return kgsl_resume_device(device);
821}
822EXPORT_SYMBOL(kgsl_resume_driver);
823
824/**
825 * kgsl_destroy_process_private() - Cleanup function to free process private
826 * @kref: - Pointer to object being destroyed's kref struct
827 * Free struct object and all other resources attached to it.
828 * Since the function can be used when not all resources inside process
829 * private have been allocated, there is a check to (before each resource
830 * cleanup) see if the struct member being cleaned is in fact allocated or not.
831 * If the value is not NULL, resource is freed.
832 */
833static void kgsl_destroy_process_private(struct kref *kref)
834{
835 struct kgsl_process_private *private = container_of(kref,
836 struct kgsl_process_private, refcount);
837
838 idr_destroy(&private->mem_idr);
839 idr_destroy(&private->syncsource_idr);
840
841 /* When using global pagetables, do not detach global pagetable */
842 if (private->pagetable->name != KGSL_MMU_GLOBAL_PT)
843 kgsl_mmu_putpagetable(private->pagetable);
844
845 kfree(private);
846}
847
848void
849kgsl_process_private_put(struct kgsl_process_private *private)
850{
851 if (private)
852 kref_put(&private->refcount, kgsl_destroy_process_private);
853}
854
855/**
856 * kgsl_process_private_find() - Find the process associated with the specified
857 * name
858 * @name: pid_t of the process to search for
859 * Return the process struct for the given ID.
860 */
861struct kgsl_process_private *kgsl_process_private_find(pid_t pid)
862{
863 struct kgsl_process_private *p, *private = NULL;
864
865 mutex_lock(&kgsl_driver.process_mutex);
866 list_for_each_entry(p, &kgsl_driver.process_list, list) {
867 if (p->pid == pid) {
868 if (kgsl_process_private_get(p))
869 private = p;
870 break;
871 }
872 }
873 mutex_unlock(&kgsl_driver.process_mutex);
874 return private;
875}
876
877static struct kgsl_process_private *kgsl_process_private_new(
878 struct kgsl_device *device)
879{
880 struct kgsl_process_private *private;
881 pid_t tgid = task_tgid_nr(current);
882
883 /* Search in the process list */
884 list_for_each_entry(private, &kgsl_driver.process_list, list) {
885 if (private->pid == tgid) {
886 if (!kgsl_process_private_get(private))
887 private = ERR_PTR(-EINVAL);
888 return private;
889 }
890 }
891
892 /* Create a new object */
893 private = kzalloc(sizeof(struct kgsl_process_private), GFP_KERNEL);
894 if (private == NULL)
895 return ERR_PTR(-ENOMEM);
896
897 kref_init(&private->refcount);
898
899 private->pid = tgid;
900 get_task_comm(private->comm, current->group_leader);
901
902 spin_lock_init(&private->mem_lock);
903 spin_lock_init(&private->syncsource_lock);
904
905 idr_init(&private->mem_idr);
906 idr_init(&private->syncsource_idr);
907
908 /* Allocate a pagetable for the new process object */
909 private->pagetable = kgsl_mmu_getpagetable(&device->mmu, tgid);
910 if (IS_ERR(private->pagetable)) {
911 int err = PTR_ERR(private->pagetable);
912
913 idr_destroy(&private->mem_idr);
914 idr_destroy(&private->syncsource_idr);
915
916 kfree(private);
917 private = ERR_PTR(err);
918 }
919
920 return private;
921}
922
923static void process_release_memory(struct kgsl_process_private *private)
924{
925 struct kgsl_mem_entry *entry;
926 int next = 0;
927
928 while (1) {
929 spin_lock(&private->mem_lock);
930 entry = idr_get_next(&private->mem_idr, &next);
931 if (entry == NULL) {
932 spin_unlock(&private->mem_lock);
933 break;
934 }
935 /*
936 * If the free pending flag is not set it means that user space
937 * did not free it's reference to this entry, in that case
938 * free a reference to this entry, other references are from
939 * within kgsl so they will be freed eventually by kgsl
940 */
941 if (!entry->pending_free) {
942 entry->pending_free = 1;
943 spin_unlock(&private->mem_lock);
944 kgsl_mem_entry_put(entry);
945 } else {
946 spin_unlock(&private->mem_lock);
947 }
948 next = next + 1;
949 }
950}
951
952static void process_release_sync_sources(struct kgsl_process_private *private)
953{
954 struct kgsl_syncsource *syncsource;
955 int next = 0;
956
957 while (1) {
958 spin_lock(&private->syncsource_lock);
959 syncsource = idr_get_next(&private->syncsource_idr, &next);
960 spin_unlock(&private->syncsource_lock);
961
962 if (syncsource == NULL)
963 break;
964
965 kgsl_syncsource_put(syncsource);
966 next = next + 1;
967 }
968}
969
970static void kgsl_process_private_close(struct kgsl_device_private *dev_priv,
971 struct kgsl_process_private *private)
972{
973 mutex_lock(&kgsl_driver.process_mutex);
974
975 if (--private->fd_count > 0) {
976 mutex_unlock(&kgsl_driver.process_mutex);
977 kgsl_process_private_put(private);
978 return;
979 }
980
981 /*
982 * If this is the last file on the process take down the debug
983 * directories and garbage collect any outstanding resources
984 */
985
986 kgsl_process_uninit_sysfs(private);
987 debugfs_remove_recursive(private->debug_root);
988
989 process_release_sync_sources(private);
990
991 /* When using global pagetables, do not detach global pagetable */
992 if (private->pagetable->name != KGSL_MMU_GLOBAL_PT)
993 kgsl_mmu_detach_pagetable(private->pagetable);
994
995 /* Remove the process struct from the master list */
996 list_del(&private->list);
997
998 /*
999 * Unlock the mutex before releasing the memory - this prevents a
1000 * deadlock with the IOMMU mutex if a page fault occurs
1001 */
1002 mutex_unlock(&kgsl_driver.process_mutex);
1003
1004 process_release_memory(private);
1005
1006 kgsl_process_private_put(private);
1007}
1008
1009
1010static struct kgsl_process_private *kgsl_process_private_open(
1011 struct kgsl_device *device)
1012{
1013 struct kgsl_process_private *private;
1014
1015 mutex_lock(&kgsl_driver.process_mutex);
1016 private = kgsl_process_private_new(device);
1017
1018 if (IS_ERR(private))
1019 goto done;
1020
1021 /*
1022 * If this is a new process create the debug directories and add it to
1023 * the process list
1024 */
1025
1026 if (private->fd_count++ == 0) {
1027 kgsl_process_init_sysfs(device, private);
1028 kgsl_process_init_debugfs(private);
1029
1030 list_add(&private->list, &kgsl_driver.process_list);
1031 }
1032
1033done:
1034 mutex_unlock(&kgsl_driver.process_mutex);
1035 return private;
1036}
1037
1038static int kgsl_close_device(struct kgsl_device *device)
1039{
1040 int result = 0;
1041
1042 mutex_lock(&device->mutex);
1043 device->open_count--;
1044 if (device->open_count == 0) {
1045
1046 /* Wait for the active count to go to 0 */
1047 kgsl_active_count_wait(device, 0);
1048
1049 /* Fail if the wait times out */
1050 BUG_ON(atomic_read(&device->active_cnt) > 0);
1051
1052 result = kgsl_pwrctrl_change_state(device, KGSL_STATE_INIT);
1053 }
1054 mutex_unlock(&device->mutex);
1055 return result;
1056
1057}
1058
1059static void device_release_contexts(struct kgsl_device_private *dev_priv)
1060{
1061 struct kgsl_device *device = dev_priv->device;
1062 struct kgsl_context *context;
1063 int next = 0;
1064 int result = 0;
1065
1066 while (1) {
1067 read_lock(&device->context_lock);
1068 context = idr_get_next(&device->context_idr, &next);
1069
1070 if (context == NULL) {
1071 read_unlock(&device->context_lock);
1072 break;
1073 } else if (context->dev_priv == dev_priv) {
1074 /*
1075 * Hold a reference to the context in case somebody
1076 * tries to put it while we are detaching
1077 */
1078 result = _kgsl_context_get(context);
1079 }
1080 read_unlock(&device->context_lock);
1081
1082 if (result) {
1083 kgsl_context_detach(context);
1084 kgsl_context_put(context);
1085 result = 0;
1086 }
1087
1088 next = next + 1;
1089 }
1090}
1091
1092static int kgsl_release(struct inode *inodep, struct file *filep)
1093{
1094 struct kgsl_device_private *dev_priv = filep->private_data;
1095 struct kgsl_device *device = dev_priv->device;
1096 int result;
1097
1098 filep->private_data = NULL;
1099
1100 /* Release the contexts for the file */
1101 device_release_contexts(dev_priv);
1102
1103 /* Close down the process wide resources for the file */
1104 kgsl_process_private_close(dev_priv, dev_priv->process_priv);
1105
1106 kfree(dev_priv);
1107
1108 result = kgsl_close_device(device);
1109 pm_runtime_put(&device->pdev->dev);
1110
1111 return result;
1112}
1113
1114static int kgsl_open_device(struct kgsl_device *device)
1115{
1116 int result = 0;
1117
1118 mutex_lock(&device->mutex);
1119 if (device->open_count == 0) {
1120 /*
1121 * active_cnt special case: we are starting up for the first
1122 * time, so use this sequence instead of the kgsl_pwrctrl_wake()
1123 * which will be called by kgsl_active_count_get().
1124 */
1125 atomic_inc(&device->active_cnt);
1126 kgsl_sharedmem_set(device, &device->memstore, 0, 0,
1127 device->memstore.size);
1128 kgsl_sharedmem_set(device, &device->scratch, 0, 0,
1129 device->scratch.size);
1130
1131 result = device->ftbl->init(device);
1132 if (result)
1133 goto err;
1134
1135 result = device->ftbl->start(device, 0);
1136 if (result)
1137 goto err;
1138 /*
1139 * Make sure the gates are open, so they don't block until
1140 * we start suspend or FT.
1141 */
1142 complete_all(&device->hwaccess_gate);
1143 kgsl_pwrctrl_change_state(device, KGSL_STATE_ACTIVE);
1144 kgsl_active_count_put(device);
1145 }
1146 device->open_count++;
1147err:
1148 if (result) {
1149 kgsl_pwrctrl_change_state(device, KGSL_STATE_INIT);
1150 atomic_dec(&device->active_cnt);
1151 }
1152
1153 mutex_unlock(&device->mutex);
1154 return result;
1155}
1156
1157static int kgsl_open(struct inode *inodep, struct file *filep)
1158{
1159 int result;
1160 struct kgsl_device_private *dev_priv;
1161 struct kgsl_device *device;
1162 unsigned int minor = iminor(inodep);
1163
1164 device = kgsl_get_minor(minor);
1165 BUG_ON(device == NULL);
1166
1167 result = pm_runtime_get_sync(&device->pdev->dev);
1168 if (result < 0) {
1169 KGSL_DRV_ERR(device,
1170 "Runtime PM: Unable to wake up the device, rc = %d\n",
1171 result);
1172 return result;
1173 }
1174 result = 0;
1175
1176 dev_priv = kzalloc(sizeof(struct kgsl_device_private), GFP_KERNEL);
1177 if (dev_priv == NULL) {
1178 result = -ENOMEM;
1179 goto err;
1180 }
1181
1182 dev_priv->device = device;
1183 filep->private_data = dev_priv;
1184
1185 result = kgsl_open_device(device);
1186 if (result)
1187 goto err;
1188
1189 /*
1190 * Get file (per process) private struct. This must be done
1191 * after the first start so that the global pagetable mappings
1192 * are set up before we create the per-process pagetable.
1193 */
1194 dev_priv->process_priv = kgsl_process_private_open(device);
1195 if (IS_ERR(dev_priv->process_priv)) {
1196 result = PTR_ERR(dev_priv->process_priv);
1197 kgsl_close_device(device);
1198 goto err;
1199 }
1200
1201err:
1202 if (result) {
1203 filep->private_data = NULL;
1204 kfree(dev_priv);
1205 pm_runtime_put(&device->pdev->dev);
1206 }
1207 return result;
1208}
1209
1210#define GPUADDR_IN_MEMDESC(_val, _memdesc) \
1211 (((_val) >= (_memdesc)->gpuaddr) && \
1212 ((_val) < ((_memdesc)->gpuaddr + (_memdesc)->size)))
1213
1214/**
1215 * kgsl_sharedmem_find() - Find a gpu memory allocation
1216 *
1217 * @private: private data for the process to check.
1218 * @gpuaddr: start address of the region
1219 *
1220 * Find a gpu allocation. Caller must kgsl_mem_entry_put()
1221 * the returned entry when finished using it.
1222 */
1223struct kgsl_mem_entry * __must_check
1224kgsl_sharedmem_find(struct kgsl_process_private *private, uint64_t gpuaddr)
1225{
1226 int ret = 0, id;
1227 struct kgsl_mem_entry *entry = NULL;
1228
1229 if (!private)
1230 return NULL;
1231
1232 if (!kgsl_mmu_gpuaddr_in_range(private->pagetable, gpuaddr))
1233 return NULL;
1234
1235 spin_lock(&private->mem_lock);
1236 idr_for_each_entry(&private->mem_idr, entry, id) {
1237 if (GPUADDR_IN_MEMDESC(gpuaddr, &entry->memdesc)) {
1238 ret = kgsl_mem_entry_get(entry);
1239 break;
1240 }
1241 }
1242 spin_unlock(&private->mem_lock);
1243
1244 return (ret == 0) ? NULL : entry;
1245}
1246EXPORT_SYMBOL(kgsl_sharedmem_find);
1247
1248struct kgsl_mem_entry * __must_check
1249kgsl_sharedmem_find_id_flags(struct kgsl_process_private *process,
1250 unsigned int id, uint64_t flags)
1251{
1252 int count = 0;
1253 struct kgsl_mem_entry *entry;
1254
1255 spin_lock(&process->mem_lock);
1256 entry = idr_find(&process->mem_idr, id);
1257 if (entry)
1258 if (!entry->pending_free &&
1259 (flags & entry->memdesc.flags) == flags)
1260 count = kgsl_mem_entry_get(entry);
1261 spin_unlock(&process->mem_lock);
1262
1263 return (count == 0) ? NULL : entry;
1264}
1265
1266/**
1267 * kgsl_sharedmem_find_id() - find a memory entry by id
1268 * @process: the owning process
1269 * @id: id to find
1270 *
1271 * @returns - the mem_entry or NULL
1272 *
1273 * Caller must kgsl_mem_entry_put() the returned entry, when finished using
1274 * it.
1275 */
1276struct kgsl_mem_entry * __must_check
1277kgsl_sharedmem_find_id(struct kgsl_process_private *process, unsigned int id)
1278{
1279 return kgsl_sharedmem_find_id_flags(process, id, 0);
1280}
1281
1282/**
1283 * kgsl_mem_entry_unset_pend() - Unset the pending free flag of an entry
1284 * @entry - The memory entry
1285 */
1286static inline void kgsl_mem_entry_unset_pend(struct kgsl_mem_entry *entry)
1287{
1288 if (entry == NULL)
1289 return;
1290 spin_lock(&entry->priv->mem_lock);
1291 entry->pending_free = 0;
1292 spin_unlock(&entry->priv->mem_lock);
1293}
1294
1295/**
1296 * kgsl_mem_entry_set_pend() - Set the pending free flag of a memory entry
1297 * @entry - The memory entry
1298 *
1299 * @returns - true if pending flag was 0 else false
1300 *
1301 * This function will set the pending free flag if it is previously unset. Used
1302 * to prevent race condition between ioctls calling free/freememontimestamp
1303 * on the same entry. Whichever thread set's the flag first will do the free.
1304 */
1305static inline bool kgsl_mem_entry_set_pend(struct kgsl_mem_entry *entry)
1306{
1307 bool ret = false;
1308
1309 if (entry == NULL)
1310 return false;
1311
1312 spin_lock(&entry->priv->mem_lock);
1313 if (!entry->pending_free) {
1314 entry->pending_free = 1;
1315 ret = true;
1316 }
1317 spin_unlock(&entry->priv->mem_lock);
1318 return ret;
1319}
1320
1321/*call all ioctl sub functions with driver locked*/
1322long kgsl_ioctl_device_getproperty(struct kgsl_device_private *dev_priv,
1323 unsigned int cmd, void *data)
1324{
1325 int result = 0;
1326 struct kgsl_device_getproperty *param = data;
1327
1328 switch (param->type) {
1329 case KGSL_PROP_VERSION:
1330 {
1331 struct kgsl_version version;
1332
1333 if (param->sizebytes != sizeof(version)) {
1334 result = -EINVAL;
1335 break;
1336 }
1337
1338 version.drv_major = KGSL_VERSION_MAJOR;
1339 version.drv_minor = KGSL_VERSION_MINOR;
1340 version.dev_major = dev_priv->device->ver_major;
1341 version.dev_minor = dev_priv->device->ver_minor;
1342
1343 if (copy_to_user(param->value, &version, sizeof(version)))
1344 result = -EFAULT;
1345
1346 break;
1347 }
1348 case KGSL_PROP_GPU_RESET_STAT:
1349 {
1350 /* Return reset status of given context and clear it */
1351 uint32_t id;
1352 struct kgsl_context *context;
1353
1354 if (param->sizebytes != sizeof(unsigned int)) {
1355 result = -EINVAL;
1356 break;
1357 }
1358 /* We expect the value passed in to contain the context id */
1359 if (copy_from_user(&id, param->value,
1360 sizeof(unsigned int))) {
1361 result = -EFAULT;
1362 break;
1363 }
1364 context = kgsl_context_get_owner(dev_priv, id);
1365 if (!context) {
1366 result = -EINVAL;
1367 break;
1368 }
1369 /*
1370 * Copy the reset status to value which also serves as
1371 * the out parameter
1372 */
1373 if (copy_to_user(param->value, &(context->reset_status),
1374 sizeof(unsigned int)))
1375 result = -EFAULT;
1376 else {
1377 /* Clear reset status once its been queried */
1378 context->reset_status = KGSL_CTX_STAT_NO_ERROR;
1379 }
1380
1381 kgsl_context_put(context);
1382 break;
1383 }
1384 default:
1385 if (is_compat_task())
1386 result = dev_priv->device->ftbl->getproperty_compat(
1387 dev_priv->device, param->type,
1388 param->value, param->sizebytes);
1389 else
1390 result = dev_priv->device->ftbl->getproperty(
1391 dev_priv->device, param->type,
1392 param->value, param->sizebytes);
1393 }
1394
1395
1396 return result;
1397}
1398
1399long kgsl_ioctl_device_setproperty(struct kgsl_device_private *dev_priv,
1400 unsigned int cmd, void *data)
1401{
1402 int result = 0;
1403 /* The getproperty struct is reused for setproperty too */
1404 struct kgsl_device_getproperty *param = data;
1405
1406 /* Reroute to compat version if coming from compat_ioctl */
1407 if (is_compat_task())
1408 result = dev_priv->device->ftbl->setproperty_compat(
1409 dev_priv, param->type, param->value,
1410 param->sizebytes);
1411 else if (dev_priv->device->ftbl->setproperty)
1412 result = dev_priv->device->ftbl->setproperty(
1413 dev_priv, param->type, param->value,
1414 param->sizebytes);
1415
1416 return result;
1417}
1418
1419long kgsl_ioctl_device_waittimestamp_ctxtid(
1420 struct kgsl_device_private *dev_priv, unsigned int cmd,
1421 void *data)
1422{
1423 struct kgsl_device_waittimestamp_ctxtid *param = data;
1424 struct kgsl_device *device = dev_priv->device;
1425 long result = -EINVAL;
1426 unsigned int temp_cur_ts = 0;
1427 struct kgsl_context *context;
1428
1429 context = kgsl_context_get_owner(dev_priv, param->context_id);
1430 if (context == NULL)
1431 return result;
1432
1433 kgsl_readtimestamp(device, context, KGSL_TIMESTAMP_RETIRED,
1434 &temp_cur_ts);
1435
1436 trace_kgsl_waittimestamp_entry(device, context->id, temp_cur_ts,
1437 param->timestamp, param->timeout);
1438
1439 result = device->ftbl->waittimestamp(device, context, param->timestamp,
1440 param->timeout);
1441
1442 kgsl_readtimestamp(device, context, KGSL_TIMESTAMP_RETIRED,
1443 &temp_cur_ts);
1444 trace_kgsl_waittimestamp_exit(device, temp_cur_ts, result);
1445
1446 kgsl_context_put(context);
1447
1448 return result;
1449}
1450
1451long kgsl_ioctl_rb_issueibcmds(struct kgsl_device_private *dev_priv,
1452 unsigned int cmd, void *data)
1453{
1454 struct kgsl_ringbuffer_issueibcmds *param = data;
1455 struct kgsl_device *device = dev_priv->device;
1456 struct kgsl_context *context;
1457 struct kgsl_drawobj *drawobj;
1458 struct kgsl_drawobj_cmd *cmdobj;
1459 long result = -EINVAL;
1460
1461 /* The legacy functions don't support synchronization commands */
1462 if ((param->flags & (KGSL_DRAWOBJ_SYNC | KGSL_DRAWOBJ_MARKER)))
1463 return -EINVAL;
1464
1465 /* Sanity check the number of IBs */
1466 if (param->flags & KGSL_DRAWOBJ_SUBMIT_IB_LIST &&
1467 (param->numibs == 0 || param->numibs > KGSL_MAX_NUMIBS))
1468 return -EINVAL;
1469
1470 /* Get the context */
1471 context = kgsl_context_get_owner(dev_priv, param->drawctxt_id);
1472 if (context == NULL)
1473 return -EINVAL;
1474
1475 cmdobj = kgsl_drawobj_cmd_create(device, context, param->flags,
1476 CMDOBJ_TYPE);
1477 if (IS_ERR(cmdobj)) {
1478 kgsl_context_put(context);
1479 return PTR_ERR(cmdobj);
1480 }
1481
1482 drawobj = DRAWOBJ(cmdobj);
1483
1484 if (param->flags & KGSL_DRAWOBJ_SUBMIT_IB_LIST)
1485 result = kgsl_drawobj_cmd_add_ibdesc_list(device, cmdobj,
1486 (void __user *) param->ibdesc_addr,
1487 param->numibs);
1488 else {
1489 struct kgsl_ibdesc ibdesc;
1490 /* Ultra legacy path */
1491
1492 ibdesc.gpuaddr = param->ibdesc_addr;
1493 ibdesc.sizedwords = param->numibs;
1494 ibdesc.ctrl = 0;
1495
1496 result = kgsl_drawobj_cmd_add_ibdesc(device, cmdobj, &ibdesc);
1497 }
1498
1499 if (result == 0)
1500 result = dev_priv->device->ftbl->queue_cmds(dev_priv, context,
1501 &drawobj, 1, &param->timestamp);
1502
1503 /*
1504 * -EPROTO is a "success" error - it just tells the user that the
1505 * context had previously faulted
1506 */
1507 if (result && result != -EPROTO)
1508 kgsl_drawobj_destroy(drawobj);
1509
1510 kgsl_context_put(context);
1511 return result;
1512}
1513
1514/* Returns 0 on failure. Returns command type(s) on success */
1515static unsigned int _process_command_input(struct kgsl_device *device,
1516 unsigned int flags, unsigned int numcmds,
1517 unsigned int numobjs, unsigned int numsyncs)
1518{
1519 if (numcmds > KGSL_MAX_NUMIBS ||
1520 numobjs > KGSL_MAX_NUMIBS ||
1521 numsyncs > KGSL_MAX_SYNCPOINTS)
1522 return 0;
1523
1524 /*
1525 * The SYNC bit is supposed to identify a dummy sync object
1526 * so warn the user if they specified any IBs with it.
1527 * A MARKER command can either have IBs or not but if the
1528 * command has 0 IBs it is automatically assumed to be a marker.
1529 */
1530
1531 /* If they specify the flag, go with what they say */
1532 if (flags & KGSL_DRAWOBJ_MARKER)
1533 return MARKEROBJ_TYPE;
1534 else if (flags & KGSL_DRAWOBJ_SYNC)
1535 return SYNCOBJ_TYPE;
1536
1537 /* If not, deduce what they meant */
1538 if (numsyncs && numcmds)
1539 return SYNCOBJ_TYPE | CMDOBJ_TYPE;
1540 else if (numsyncs)
1541 return SYNCOBJ_TYPE;
1542 else if (numcmds)
1543 return CMDOBJ_TYPE;
1544 else if (numcmds == 0)
1545 return MARKEROBJ_TYPE;
1546
1547 return 0;
1548}
1549
1550long kgsl_ioctl_submit_commands(struct kgsl_device_private *dev_priv,
1551 unsigned int cmd, void *data)
1552{
1553 struct kgsl_submit_commands *param = data;
1554 struct kgsl_device *device = dev_priv->device;
1555 struct kgsl_context *context;
1556 struct kgsl_drawobj *drawobj[2];
1557 unsigned int type;
1558 long result;
1559 unsigned int i = 0;
1560
1561 type = _process_command_input(device, param->flags, param->numcmds, 0,
1562 param->numsyncs);
1563 if (!type)
1564 return -EINVAL;
1565
1566 context = kgsl_context_get_owner(dev_priv, param->context_id);
1567 if (context == NULL)
1568 return -EINVAL;
1569
1570 if (type & SYNCOBJ_TYPE) {
1571 struct kgsl_drawobj_sync *syncobj =
1572 kgsl_drawobj_sync_create(device, context);
1573 if (IS_ERR(syncobj)) {
1574 result = PTR_ERR(syncobj);
1575 goto done;
1576 }
1577
1578 drawobj[i++] = DRAWOBJ(syncobj);
1579
1580 result = kgsl_drawobj_sync_add_syncpoints(device, syncobj,
1581 param->synclist, param->numsyncs);
1582 if (result)
1583 goto done;
1584 }
1585
1586 if (type & (CMDOBJ_TYPE | MARKEROBJ_TYPE)) {
1587 struct kgsl_drawobj_cmd *cmdobj =
1588 kgsl_drawobj_cmd_create(device,
1589 context, param->flags, type);
1590 if (IS_ERR(cmdobj)) {
1591 result = PTR_ERR(cmdobj);
1592 goto done;
1593 }
1594
1595 drawobj[i++] = DRAWOBJ(cmdobj);
1596
1597 result = kgsl_drawobj_cmd_add_ibdesc_list(device, cmdobj,
1598 param->cmdlist, param->numcmds);
1599 if (result)
1600 goto done;
1601
1602 /* If no profiling buffer was specified, clear the flag */
1603 if (cmdobj->profiling_buf_entry == NULL)
1604 DRAWOBJ(cmdobj)->flags &= ~KGSL_DRAWOBJ_PROFILING;
1605 }
1606
1607 result = device->ftbl->queue_cmds(dev_priv, context, drawobj,
1608 i, &param->timestamp);
1609
1610done:
1611 /*
1612 * -EPROTO is a "success" error - it just tells the user that the
1613 * context had previously faulted
1614 */
1615 if (result && result != -EPROTO)
1616 while (i--)
1617 kgsl_drawobj_destroy(drawobj[i]);
1618
1619
1620 kgsl_context_put(context);
1621 return result;
1622}
1623
1624long kgsl_ioctl_gpu_command(struct kgsl_device_private *dev_priv,
1625 unsigned int cmd, void *data)
1626{
1627 struct kgsl_gpu_command *param = data;
1628 struct kgsl_device *device = dev_priv->device;
1629 struct kgsl_context *context;
1630 struct kgsl_drawobj *drawobj[2];
1631 unsigned int type;
1632 long result;
1633 unsigned int i = 0;
1634
1635 type = _process_command_input(device, param->flags, param->numcmds,
1636 param->numobjs, param->numsyncs);
1637 if (!type)
1638 return -EINVAL;
1639
1640 context = kgsl_context_get_owner(dev_priv, param->context_id);
1641 if (context == NULL)
1642 return -EINVAL;
1643
1644 if (type & SYNCOBJ_TYPE) {
1645 struct kgsl_drawobj_sync *syncobj =
1646 kgsl_drawobj_sync_create(device, context);
1647
1648 if (IS_ERR(syncobj)) {
1649 result = PTR_ERR(syncobj);
1650 goto done;
1651 }
1652
1653 drawobj[i++] = DRAWOBJ(syncobj);
1654
1655 result = kgsl_drawobj_sync_add_synclist(device, syncobj,
1656 to_user_ptr(param->synclist),
1657 param->syncsize, param->numsyncs);
1658 if (result)
1659 goto done;
1660 }
1661
1662 if (type & (CMDOBJ_TYPE | MARKEROBJ_TYPE)) {
1663 struct kgsl_drawobj_cmd *cmdobj =
1664 kgsl_drawobj_cmd_create(device,
1665 context, param->flags, type);
1666
1667 if (IS_ERR(cmdobj)) {
1668 result = PTR_ERR(cmdobj);
1669 goto done;
1670 }
1671
1672 drawobj[i++] = DRAWOBJ(cmdobj);
1673
1674 result = kgsl_drawobj_cmd_add_cmdlist(device, cmdobj,
1675 to_user_ptr(param->cmdlist),
1676 param->cmdsize, param->numcmds);
1677 if (result)
1678 goto done;
1679
1680 result = kgsl_drawobj_cmd_add_memlist(device, cmdobj,
1681 to_user_ptr(param->objlist),
1682 param->objsize, param->numobjs);
1683 if (result)
1684 goto done;
1685
1686 /* If no profiling buffer was specified, clear the flag */
1687 if (cmdobj->profiling_buf_entry == NULL)
1688 DRAWOBJ(cmdobj)->flags &= ~KGSL_DRAWOBJ_PROFILING;
1689 }
1690
1691 result = device->ftbl->queue_cmds(dev_priv, context, drawobj,
1692 i, &param->timestamp);
1693
1694done:
1695 /*
1696 * -EPROTO is a "success" error - it just tells the user that the
1697 * context had previously faulted
1698 */
1699 if (result && result != -EPROTO)
1700 while (i--)
1701 kgsl_drawobj_destroy(drawobj[i]);
1702
1703 kgsl_context_put(context);
1704 return result;
1705}
1706
1707long kgsl_ioctl_cmdstream_readtimestamp_ctxtid(struct kgsl_device_private
1708 *dev_priv, unsigned int cmd,
1709 void *data)
1710{
1711 struct kgsl_cmdstream_readtimestamp_ctxtid *param = data;
1712 struct kgsl_device *device = dev_priv->device;
1713 struct kgsl_context *context;
1714 long result = -EINVAL;
1715
1716 mutex_lock(&device->mutex);
1717 context = kgsl_context_get_owner(dev_priv, param->context_id);
1718
1719 if (context) {
1720 result = kgsl_readtimestamp(device, context,
1721 param->type, &param->timestamp);
1722
1723 trace_kgsl_readtimestamp(device, context->id,
1724 param->type, param->timestamp);
1725 }
1726
1727 kgsl_context_put(context);
1728 mutex_unlock(&device->mutex);
1729 return result;
1730}
1731
1732long kgsl_ioctl_drawctxt_create(struct kgsl_device_private *dev_priv,
1733 unsigned int cmd, void *data)
1734{
1735 int result = 0;
1736 struct kgsl_drawctxt_create *param = data;
1737 struct kgsl_context *context = NULL;
1738 struct kgsl_device *device = dev_priv->device;
1739
1740 context = device->ftbl->drawctxt_create(dev_priv, &param->flags);
1741 if (IS_ERR(context)) {
1742 result = PTR_ERR(context);
1743 goto done;
1744 }
1745 trace_kgsl_context_create(dev_priv->device, context, param->flags);
1746
1747 /* Commit the pointer to the context in context_idr */
1748 write_lock(&device->context_lock);
1749 idr_replace(&device->context_idr, context, context->id);
1750 write_unlock(&device->context_lock);
1751
1752 param->drawctxt_id = context->id;
1753done:
1754 return result;
1755}
1756
1757long kgsl_ioctl_drawctxt_destroy(struct kgsl_device_private *dev_priv,
1758 unsigned int cmd, void *data)
1759{
1760 struct kgsl_drawctxt_destroy *param = data;
1761 struct kgsl_context *context;
1762
1763 context = kgsl_context_get_owner(dev_priv, param->drawctxt_id);
1764 if (context == NULL)
1765 return -EINVAL;
1766
1767 kgsl_context_detach(context);
1768 kgsl_context_put(context);
1769
1770 return 0;
1771}
1772
1773static long gpumem_free_entry(struct kgsl_mem_entry *entry)
1774{
1775 pid_t ptname = 0;
1776
1777 if (!kgsl_mem_entry_set_pend(entry))
1778 return -EBUSY;
1779
1780 trace_kgsl_mem_free(entry);
1781
1782 if (entry->memdesc.pagetable != NULL)
1783 ptname = entry->memdesc.pagetable->name;
1784
1785 kgsl_memfree_add(entry->priv->pid, ptname, entry->memdesc.gpuaddr,
1786 entry->memdesc.size, entry->memdesc.flags);
1787
1788 kgsl_mem_entry_put(entry);
1789
1790 return 0;
1791}
1792
1793static void gpumem_free_func(struct kgsl_device *device,
1794 struct kgsl_event_group *group, void *priv, int ret)
1795{
1796 struct kgsl_context *context = group->context;
1797 struct kgsl_mem_entry *entry = priv;
1798 unsigned int timestamp;
1799
1800 kgsl_readtimestamp(device, context, KGSL_TIMESTAMP_RETIRED, &timestamp);
1801
1802 /* Free the memory for all event types */
1803 trace_kgsl_mem_timestamp_free(device, entry, KGSL_CONTEXT_ID(context),
1804 timestamp, 0);
1805 kgsl_mem_entry_put(entry);
1806}
1807
1808static long gpumem_free_entry_on_timestamp(struct kgsl_device *device,
1809 struct kgsl_mem_entry *entry,
1810 struct kgsl_context *context, unsigned int timestamp)
1811{
1812 int ret;
1813 unsigned int temp;
1814
1815 if (!kgsl_mem_entry_set_pend(entry))
1816 return -EBUSY;
1817
1818 kgsl_readtimestamp(device, context, KGSL_TIMESTAMP_RETIRED, &temp);
1819 trace_kgsl_mem_timestamp_queue(device, entry, context->id, temp,
1820 timestamp);
1821 ret = kgsl_add_event(device, &context->events,
1822 timestamp, gpumem_free_func, entry);
1823
1824 if (ret)
1825 kgsl_mem_entry_unset_pend(entry);
1826
1827 return ret;
1828}
1829
1830long kgsl_ioctl_sharedmem_free(struct kgsl_device_private *dev_priv,
1831 unsigned int cmd, void *data)
1832{
1833 struct kgsl_sharedmem_free *param = data;
1834 struct kgsl_process_private *private = dev_priv->process_priv;
1835 struct kgsl_mem_entry *entry;
1836 long ret;
1837
1838 entry = kgsl_sharedmem_find(private, (uint64_t) param->gpuaddr);
1839 if (entry == NULL)
1840 return -EINVAL;
1841
1842 ret = gpumem_free_entry(entry);
1843 kgsl_mem_entry_put(entry);
1844
1845 return ret;
1846}
1847
1848long kgsl_ioctl_gpumem_free_id(struct kgsl_device_private *dev_priv,
1849 unsigned int cmd, void *data)
1850{
1851 struct kgsl_gpumem_free_id *param = data;
1852 struct kgsl_process_private *private = dev_priv->process_priv;
1853 struct kgsl_mem_entry *entry;
1854 long ret;
1855
1856 entry = kgsl_sharedmem_find_id(private, param->id);
1857 if (entry == NULL)
1858 return -EINVAL;
1859
1860 ret = gpumem_free_entry(entry);
1861 kgsl_mem_entry_put(entry);
1862
1863 return ret;
1864}
1865
1866static long gpuobj_free_on_timestamp(struct kgsl_device_private *dev_priv,
1867 struct kgsl_mem_entry *entry, struct kgsl_gpuobj_free *param)
1868{
1869 struct kgsl_gpu_event_timestamp event;
1870 struct kgsl_context *context;
1871 long ret;
1872
1873 memset(&event, 0, sizeof(event));
1874
1875 ret = _copy_from_user(&event, to_user_ptr(param->priv),
1876 sizeof(event), param->len);
1877 if (ret)
1878 return ret;
1879
1880 if (event.context_id == 0)
1881 return -EINVAL;
1882
1883 context = kgsl_context_get_owner(dev_priv, event.context_id);
1884 if (context == NULL)
1885 return -EINVAL;
1886
1887 ret = gpumem_free_entry_on_timestamp(dev_priv->device, entry, context,
1888 event.timestamp);
1889
1890 kgsl_context_put(context);
1891 return ret;
1892}
1893
1894static void gpuobj_free_fence_func(void *priv)
1895{
1896 struct kgsl_mem_entry *entry = priv;
1897
1898 INIT_WORK(&entry->work, _deferred_put);
1899 queue_work(kgsl_driver.mem_workqueue, &entry->work);
1900}
1901
1902static long gpuobj_free_on_fence(struct kgsl_device_private *dev_priv,
1903 struct kgsl_mem_entry *entry, struct kgsl_gpuobj_free *param)
1904{
1905 struct kgsl_sync_fence_waiter *handle;
1906 struct kgsl_gpu_event_fence event;
1907 long ret;
1908
1909 if (!kgsl_mem_entry_set_pend(entry))
1910 return -EBUSY;
1911
1912 memset(&event, 0, sizeof(event));
1913
1914 ret = _copy_from_user(&event, to_user_ptr(param->priv),
1915 sizeof(event), param->len);
1916 if (ret) {
1917 kgsl_mem_entry_unset_pend(entry);
1918 return ret;
1919 }
1920
1921 if (event.fd < 0) {
1922 kgsl_mem_entry_unset_pend(entry);
1923 return -EINVAL;
1924 }
1925
1926 handle = kgsl_sync_fence_async_wait(event.fd,
1927 gpuobj_free_fence_func, entry);
1928
1929 /* if handle is NULL the fence has already signaled */
1930 if (handle == NULL)
1931 return gpumem_free_entry(entry);
1932
1933 if (IS_ERR(handle)) {
1934 kgsl_mem_entry_unset_pend(entry);
1935 return PTR_ERR(handle);
1936 }
1937
1938 return 0;
1939}
1940
1941long kgsl_ioctl_gpuobj_free(struct kgsl_device_private *dev_priv,
1942 unsigned int cmd, void *data)
1943{
1944 struct kgsl_gpuobj_free *param = data;
1945 struct kgsl_process_private *private = dev_priv->process_priv;
1946 struct kgsl_mem_entry *entry;
1947 long ret;
1948
1949 entry = kgsl_sharedmem_find_id(private, param->id);
1950 if (entry == NULL)
1951 return -EINVAL;
1952
1953 /* If no event is specified then free immediately */
1954 if (!(param->flags & KGSL_GPUOBJ_FREE_ON_EVENT))
1955 ret = gpumem_free_entry(entry);
1956 else if (param->type == KGSL_GPU_EVENT_TIMESTAMP)
1957 ret = gpuobj_free_on_timestamp(dev_priv, entry, param);
1958 else if (param->type == KGSL_GPU_EVENT_FENCE)
1959 ret = gpuobj_free_on_fence(dev_priv, entry, param);
1960 else
1961 ret = -EINVAL;
1962
1963 kgsl_mem_entry_put(entry);
1964 return ret;
1965}
1966
1967long kgsl_ioctl_cmdstream_freememontimestamp_ctxtid(
1968 struct kgsl_device_private *dev_priv,
1969 unsigned int cmd, void *data)
1970{
1971 struct kgsl_cmdstream_freememontimestamp_ctxtid *param = data;
1972 struct kgsl_context *context = NULL;
1973 struct kgsl_mem_entry *entry;
1974 long ret = -EINVAL;
1975
1976 if (param->type != KGSL_TIMESTAMP_RETIRED)
1977 return -EINVAL;
1978
1979 context = kgsl_context_get_owner(dev_priv, param->context_id);
1980 if (context == NULL)
1981 return -EINVAL;
1982
1983 entry = kgsl_sharedmem_find(dev_priv->process_priv,
1984 (uint64_t) param->gpuaddr);
1985 if (entry == NULL) {
1986 kgsl_context_put(context);
1987 return -EINVAL;
1988 }
1989
1990 ret = gpumem_free_entry_on_timestamp(dev_priv->device, entry,
1991 context, param->timestamp);
1992
1993 kgsl_mem_entry_put(entry);
1994 kgsl_context_put(context);
1995
1996 return ret;
1997}
1998
1999static inline int _check_region(unsigned long start, unsigned long size,
2000 uint64_t len)
2001{
2002 uint64_t end = ((uint64_t) start) + size;
2003
2004 return (end > len);
2005}
2006
2007static int check_vma_flags(struct vm_area_struct *vma,
2008 unsigned int flags)
2009{
2010 unsigned long flags_requested = (VM_READ | VM_WRITE);
2011
2012 if (flags & KGSL_MEMFLAGS_GPUREADONLY)
2013 flags_requested &= ~VM_WRITE;
2014
2015 if ((vma->vm_flags & flags_requested) == flags_requested)
2016 return 0;
2017
2018 return -EFAULT;
2019}
2020
2021static int check_vma(struct vm_area_struct *vma, struct file *vmfile,
2022 struct kgsl_memdesc *memdesc)
2023{
2024 if (vma == NULL || vma->vm_file != vmfile)
2025 return -EINVAL;
2026
2027 /* userspace may not know the size, in which case use the whole vma */
2028 if (memdesc->size == 0)
2029 memdesc->size = vma->vm_end - vma->vm_start;
2030 /* range checking */
2031 if (vma->vm_start != memdesc->useraddr ||
2032 (memdesc->useraddr + memdesc->size) != vma->vm_end)
2033 return -EINVAL;
2034 return check_vma_flags(vma, memdesc->flags);
2035}
2036
2037static int memdesc_sg_virt(struct kgsl_memdesc *memdesc, struct file *vmfile)
2038{
2039 int ret = 0;
2040 long npages = 0, i;
2041 size_t sglen = (size_t) (memdesc->size / PAGE_SIZE);
2042 struct page **pages = NULL;
2043 int write = ((memdesc->flags & KGSL_MEMFLAGS_GPUREADONLY) ? 0 :
2044 FOLL_WRITE);
2045
2046 if (sglen == 0 || sglen >= LONG_MAX)
2047 return -EINVAL;
2048
2049 pages = kgsl_malloc(sglen * sizeof(struct page *));
2050 if (pages == NULL)
2051 return -ENOMEM;
2052
2053 memdesc->sgt = kmalloc(sizeof(struct sg_table), GFP_KERNEL);
2054 if (memdesc->sgt == NULL) {
2055 ret = -ENOMEM;
2056 goto out;
2057 }
2058
2059 down_read(&current->mm->mmap_sem);
2060 /* If we have vmfile, make sure we map the correct vma and map it all */
2061 if (vmfile != NULL)
2062 ret = check_vma(find_vma(current->mm, memdesc->useraddr),
2063 vmfile, memdesc);
2064
2065 if (ret == 0) {
2066 npages = get_user_pages(memdesc->useraddr,
2067 sglen, write, pages, NULL);
2068 ret = (npages < 0) ? (int)npages : 0;
2069 }
2070 up_read(&current->mm->mmap_sem);
2071
2072 if (ret)
2073 goto out;
2074
2075 if ((unsigned long) npages != sglen) {
2076 ret = -EINVAL;
2077 goto out;
2078 }
2079
2080 ret = sg_alloc_table_from_pages(memdesc->sgt, pages, npages,
2081 0, memdesc->size, GFP_KERNEL);
2082out:
2083 if (ret) {
2084 for (i = 0; i < npages; i++)
2085 put_page(pages[i]);
2086
2087 kfree(memdesc->sgt);
2088 memdesc->sgt = NULL;
2089 }
2090 kgsl_free(pages);
2091 return ret;
2092}
2093
2094static int kgsl_setup_anon_useraddr(struct kgsl_pagetable *pagetable,
2095 struct kgsl_mem_entry *entry, unsigned long hostptr,
2096 size_t offset, size_t size)
2097{
2098 /* Map an anonymous memory chunk */
2099
2100 if (size == 0 || offset != 0 ||
2101 !IS_ALIGNED(size, PAGE_SIZE))
2102 return -EINVAL;
2103
2104 entry->memdesc.pagetable = pagetable;
2105 entry->memdesc.size = (uint64_t) size;
2106 entry->memdesc.useraddr = hostptr;
2107 entry->memdesc.flags |= KGSL_MEMFLAGS_USERMEM_ADDR;
2108
2109 if (kgsl_memdesc_use_cpu_map(&entry->memdesc)) {
2110 int ret;
2111
2112 /* Register the address in the database */
2113 ret = kgsl_mmu_set_svm_region(pagetable,
2114 (uint64_t) entry->memdesc.useraddr, (uint64_t) size);
2115
2116 if (ret)
2117 return ret;
2118
2119 entry->memdesc.gpuaddr = (uint64_t) entry->memdesc.useraddr;
2120 }
2121
2122 return memdesc_sg_virt(&entry->memdesc, NULL);
2123}
2124
2125#ifdef CONFIG_DMA_SHARED_BUFFER
2126static int match_file(const void *p, struct file *file, unsigned int fd)
2127{
2128 /*
2129 * We must return fd + 1 because iterate_fd stops searching on
2130 * non-zero return, but 0 is a valid fd.
2131 */
2132 return (p == file) ? (fd + 1) : 0;
2133}
2134
2135static void _setup_cache_mode(struct kgsl_mem_entry *entry,
2136 struct vm_area_struct *vma)
2137{
2138 unsigned int mode;
2139 pgprot_t pgprot = vma->vm_page_prot;
2140
2141 if (pgprot_val(pgprot) == pgprot_val(pgprot_noncached(pgprot)))
2142 mode = KGSL_CACHEMODE_UNCACHED;
2143 else if (pgprot_val(pgprot) == pgprot_val(pgprot_writecombine(pgprot)))
2144 mode = KGSL_CACHEMODE_WRITECOMBINE;
2145 else
2146 mode = KGSL_CACHEMODE_WRITEBACK;
2147
2148 entry->memdesc.flags |= (mode << KGSL_CACHEMODE_SHIFT);
2149}
2150
2151static int kgsl_setup_dma_buf(struct kgsl_device *device,
2152 struct kgsl_pagetable *pagetable,
2153 struct kgsl_mem_entry *entry,
2154 struct dma_buf *dmabuf);
2155
2156static int kgsl_setup_dmabuf_useraddr(struct kgsl_device *device,
2157 struct kgsl_pagetable *pagetable,
2158 struct kgsl_mem_entry *entry, unsigned long hostptr)
2159{
2160 struct vm_area_struct *vma;
2161 struct dma_buf *dmabuf = NULL;
2162 int ret;
2163
2164 /*
2165 * Find the VMA containing this pointer and figure out if it
2166 * is a dma-buf.
2167 */
2168 down_read(&current->mm->mmap_sem);
2169 vma = find_vma(current->mm, hostptr);
2170
2171 if (vma && vma->vm_file) {
2172 int fd;
2173
2174 ret = check_vma_flags(vma, entry->memdesc.flags);
2175 if (ret) {
2176 up_read(&current->mm->mmap_sem);
2177 return ret;
2178 }
2179
2180 /*
2181 * Check to see that this isn't our own memory that we have
2182 * already mapped
2183 */
2184 if (vma->vm_file->f_op == &kgsl_fops) {
2185 up_read(&current->mm->mmap_sem);
2186 return -EFAULT;
2187 }
2188
2189 /* Look for the fd that matches this the vma file */
2190 fd = iterate_fd(current->files, 0, match_file, vma->vm_file);
2191 if (fd != 0)
2192 dmabuf = dma_buf_get(fd - 1);
2193 }
2194 up_read(&current->mm->mmap_sem);
2195
2196 if (IS_ERR_OR_NULL(dmabuf))
2197 return dmabuf ? PTR_ERR(dmabuf) : -ENODEV;
2198
2199 ret = kgsl_setup_dma_buf(device, pagetable, entry, dmabuf);
2200 if (ret) {
2201 dma_buf_put(dmabuf);
2202 return ret;
2203 }
2204
2205 /* Setup the user addr/cache mode for cache operations */
2206 entry->memdesc.useraddr = hostptr;
2207 _setup_cache_mode(entry, vma);
2208
2209 return 0;
2210}
2211#else
2212static int kgsl_setup_dmabuf_useraddr(struct kgsl_device *device,
2213 struct kgsl_pagetable *pagetable,
2214 struct kgsl_mem_entry *entry, unsigned long hostptr)
2215{
2216 return -ENODEV;
2217}
2218#endif
2219
2220static int kgsl_setup_useraddr(struct kgsl_device *device,
2221 struct kgsl_pagetable *pagetable,
2222 struct kgsl_mem_entry *entry,
2223 unsigned long hostptr, size_t offset, size_t size)
2224{
2225 int ret;
2226
2227 if (hostptr == 0 || !IS_ALIGNED(hostptr, PAGE_SIZE))
2228 return -EINVAL;
2229
2230 /* Try to set up a dmabuf - if it returns -ENODEV assume anonymous */
2231 ret = kgsl_setup_dmabuf_useraddr(device, pagetable, entry, hostptr);
2232 if (ret != -ENODEV)
2233 return ret;
2234
2235 /* Okay - lets go legacy */
2236 return kgsl_setup_anon_useraddr(pagetable, entry,
2237 hostptr, offset, size);
2238}
2239
2240static long _gpuobj_map_useraddr(struct kgsl_device *device,
2241 struct kgsl_pagetable *pagetable,
2242 struct kgsl_mem_entry *entry,
2243 struct kgsl_gpuobj_import *param)
2244{
2245 struct kgsl_gpuobj_import_useraddr useraddr;
2246 int ret;
2247
2248 param->flags &= KGSL_MEMFLAGS_GPUREADONLY
2249 | KGSL_CACHEMODE_MASK
2250 | KGSL_MEMTYPE_MASK
2251 | KGSL_MEMFLAGS_FORCE_32BIT;
2252
2253 /* Specifying SECURE is an explicit error */
2254 if (param->flags & KGSL_MEMFLAGS_SECURE)
2255 return -ENOTSUPP;
2256
2257 ret = _copy_from_user(&useraddr,
2258 to_user_ptr(param->priv), sizeof(useraddr),
2259 param->priv_len);
2260 if (ret)
2261 return ret;
2262
2263 /* Verify that the virtaddr and len are within bounds */
2264 if (useraddr.virtaddr > ULONG_MAX)
2265 return -EINVAL;
2266
2267 return kgsl_setup_useraddr(device, pagetable, entry,
2268 (unsigned long) useraddr.virtaddr, 0, param->priv_len);
2269}
2270
2271#ifdef CONFIG_DMA_SHARED_BUFFER
2272static long _gpuobj_map_dma_buf(struct kgsl_device *device,
2273 struct kgsl_pagetable *pagetable,
2274 struct kgsl_mem_entry *entry,
2275 struct kgsl_gpuobj_import *param,
2276 int *fd)
2277{
2278 struct kgsl_gpuobj_import_dma_buf buf;
2279 struct dma_buf *dmabuf;
2280 int ret;
2281
2282 /*
2283 * If content protection is not enabled and secure buffer
2284 * is requested to be mapped return error.
2285 */
2286 if (entry->memdesc.flags & KGSL_MEMFLAGS_SECURE) {
2287 if (!kgsl_mmu_is_secured(&device->mmu)) {
2288 dev_WARN_ONCE(device->dev, 1,
2289 "Secure buffer not supported");
2290 return -ENOTSUPP;
2291 }
2292
2293 entry->memdesc.priv |= KGSL_MEMDESC_SECURE;
2294 }
2295
2296 ret = _copy_from_user(&buf, to_user_ptr(param->priv),
2297 sizeof(buf), param->priv_len);
2298 if (ret)
2299 return ret;
2300
2301 if (buf.fd < 0)
2302 return -EINVAL;
2303
2304 *fd = buf.fd;
2305 dmabuf = dma_buf_get(buf.fd);
2306
2307 if (IS_ERR_OR_NULL(dmabuf))
2308 return (dmabuf == NULL) ? -EINVAL : PTR_ERR(dmabuf);
2309
2310 ret = kgsl_setup_dma_buf(device, pagetable, entry, dmabuf);
2311 if (ret)
2312 dma_buf_put(dmabuf);
2313
2314 return ret;
2315}
2316#else
2317static long _gpuobj_map_dma_buf(struct kgsl_device *device,
2318 struct kgsl_pagetable *pagetable,
2319 struct kgsl_mem_entry *entry,
2320 struct kgsl_gpuobj_import *param,
2321 int *fd)
2322{
2323 return -EINVAL;
2324}
2325#endif
2326
2327long kgsl_ioctl_gpuobj_import(struct kgsl_device_private *dev_priv,
2328 unsigned int cmd, void *data)
2329{
2330 struct kgsl_process_private *private = dev_priv->process_priv;
2331 struct kgsl_gpuobj_import *param = data;
2332 struct kgsl_mem_entry *entry;
2333 int ret, fd = -1;
2334 struct kgsl_mmu *mmu = &dev_priv->device->mmu;
2335
2336 entry = kgsl_mem_entry_create();
2337 if (entry == NULL)
2338 return -ENOMEM;
2339
2340 param->flags &= KGSL_MEMFLAGS_GPUREADONLY
2341 | KGSL_MEMTYPE_MASK
2342 | KGSL_MEMALIGN_MASK
2343 | KGSL_MEMFLAGS_USE_CPU_MAP
2344 | KGSL_MEMFLAGS_SECURE
2345 | KGSL_MEMFLAGS_FORCE_32BIT;
2346
2347 entry->memdesc.flags = param->flags;
2348
2349 if (MMU_FEATURE(mmu, KGSL_MMU_NEED_GUARD_PAGE))
2350 entry->memdesc.priv |= KGSL_MEMDESC_GUARD_PAGE;
2351
2352 if (param->type == KGSL_USER_MEM_TYPE_ADDR)
2353 ret = _gpuobj_map_useraddr(dev_priv->device, private->pagetable,
2354 entry, param);
2355 else if (param->type == KGSL_USER_MEM_TYPE_DMABUF)
2356 ret = _gpuobj_map_dma_buf(dev_priv->device, private->pagetable,
2357 entry, param, &fd);
2358 else
2359 ret = -ENOTSUPP;
2360
2361 if (ret)
2362 goto out;
2363
2364 if (entry->memdesc.size >= SZ_1M)
2365 kgsl_memdesc_set_align(&entry->memdesc, ilog2(SZ_1M));
2366 else if (entry->memdesc.size >= SZ_64K)
2367 kgsl_memdesc_set_align(&entry->memdesc, ilog2(SZ_64K));
2368
2369 param->flags = entry->memdesc.flags;
2370
2371 ret = kgsl_mem_entry_attach_process(dev_priv->device, private, entry);
2372 if (ret)
2373 goto unmap;
2374
2375 param->id = entry->id;
2376
2377 KGSL_STATS_ADD(entry->memdesc.size, &kgsl_driver.stats.mapped,
2378 &kgsl_driver.stats.mapped_max);
2379
2380 kgsl_process_add_stats(private,
2381 kgsl_memdesc_usermem_type(&entry->memdesc),
2382 entry->memdesc.size);
2383
2384 trace_kgsl_mem_map(entry, fd);
2385
2386 kgsl_mem_entry_commit_process(entry);
2387 return 0;
2388
2389unmap:
2390 if (param->type == KGSL_USER_MEM_TYPE_DMABUF) {
2391 kgsl_destroy_ion(entry->priv_data);
2392 entry->memdesc.sgt = NULL;
2393 }
2394
2395 kgsl_sharedmem_free(&entry->memdesc);
2396
2397out:
2398 kfree(entry);
2399 return ret;
2400}
2401
2402static long _map_usermem_addr(struct kgsl_device *device,
2403 struct kgsl_pagetable *pagetable, struct kgsl_mem_entry *entry,
2404 unsigned long hostptr, size_t offset, size_t size)
2405{
2406 if (!MMU_FEATURE(&device->mmu, KGSL_MMU_PAGED))
2407 return -EINVAL;
2408
2409 /* No CPU mapped buffer could ever be secure */
2410 if (entry->memdesc.flags & KGSL_MEMFLAGS_SECURE)
2411 return -EINVAL;
2412
2413 return kgsl_setup_useraddr(device, pagetable, entry, hostptr,
2414 offset, size);
2415}
2416
2417#ifdef CONFIG_DMA_SHARED_BUFFER
2418static int _map_usermem_dma_buf(struct kgsl_device *device,
2419 struct kgsl_pagetable *pagetable,
2420 struct kgsl_mem_entry *entry,
2421 unsigned int fd)
2422{
2423 int ret;
2424 struct dma_buf *dmabuf;
2425
2426 /*
2427 * If content protection is not enabled and secure buffer
2428 * is requested to be mapped return error.
2429 */
2430
2431 if (entry->memdesc.flags & KGSL_MEMFLAGS_SECURE) {
2432 if (!kgsl_mmu_is_secured(&device->mmu)) {
2433 dev_WARN_ONCE(device->dev, 1,
2434 "Secure buffer not supported");
2435 return -EINVAL;
2436 }
2437
2438 entry->memdesc.priv |= KGSL_MEMDESC_SECURE;
2439 }
2440
2441 dmabuf = dma_buf_get(fd);
2442 if (IS_ERR_OR_NULL(dmabuf)) {
2443 ret = PTR_ERR(dmabuf);
2444 return ret ? ret : -EINVAL;
2445 }
2446 ret = kgsl_setup_dma_buf(device, pagetable, entry, dmabuf);
2447 if (ret)
2448 dma_buf_put(dmabuf);
2449 return ret;
2450}
2451#else
2452static int _map_usermem_dma_buf(struct kgsl_device *device,
2453 struct kgsl_pagetable *pagetable,
2454 struct kgsl_mem_entry *entry,
2455 unsigned int fd)
2456{
2457 return -EINVAL;
2458}
2459#endif
2460
2461#ifdef CONFIG_DMA_SHARED_BUFFER
2462static int kgsl_setup_dma_buf(struct kgsl_device *device,
2463 struct kgsl_pagetable *pagetable,
2464 struct kgsl_mem_entry *entry,
2465 struct dma_buf *dmabuf)
2466{
2467 int ret = 0;
2468 struct scatterlist *s;
2469 struct sg_table *sg_table;
2470 struct dma_buf_attachment *attach = NULL;
2471 struct kgsl_dma_buf_meta *meta;
2472
2473 meta = kzalloc(sizeof(*meta), GFP_KERNEL);
2474 if (!meta)
2475 return -ENOMEM;
2476
2477 attach = dma_buf_attach(dmabuf, device->dev);
2478 if (IS_ERR_OR_NULL(attach)) {
2479 ret = attach ? PTR_ERR(attach) : -EINVAL;
2480 goto out;
2481 }
2482
2483 meta->dmabuf = dmabuf;
2484 meta->attach = attach;
2485
2486 attach->priv = entry;
2487
2488 entry->priv_data = meta;
2489 entry->memdesc.pagetable = pagetable;
2490 entry->memdesc.size = 0;
2491 /* USE_CPU_MAP is not impemented for ION. */
2492 entry->memdesc.flags &= ~((uint64_t) KGSL_MEMFLAGS_USE_CPU_MAP);
2493 entry->memdesc.flags |= KGSL_MEMFLAGS_USERMEM_ION;
2494
2495 sg_table = dma_buf_map_attachment(attach, DMA_TO_DEVICE);
2496
2497 if (IS_ERR_OR_NULL(sg_table)) {
2498 ret = PTR_ERR(sg_table);
2499 goto out;
2500 }
2501
2502 meta->table = sg_table;
2503 entry->priv_data = meta;
2504 entry->memdesc.sgt = sg_table;
2505
2506 /* Calculate the size of the memdesc from the sglist */
2507 for (s = entry->memdesc.sgt->sgl; s != NULL; s = sg_next(s)) {
2508 int priv = (entry->memdesc.priv & KGSL_MEMDESC_SECURE) ? 1 : 0;
2509
2510 /*
2511 * Check that each chunk of of the sg table matches the secure
2512 * flag.
2513 */
2514
2515 if (PagePrivate(sg_page(s)) != priv) {
2516 ret = -EPERM;
2517 goto out;
2518 }
2519
2520 entry->memdesc.size += (uint64_t) s->length;
2521 }
2522
2523 entry->memdesc.size = PAGE_ALIGN(entry->memdesc.size);
2524
2525out:
2526 if (ret) {
2527 if (!IS_ERR_OR_NULL(attach))
2528 dma_buf_detach(dmabuf, attach);
2529
2530
2531 kfree(meta);
2532 }
2533
2534 return ret;
2535}
2536#endif
2537
2538#ifdef CONFIG_DMA_SHARED_BUFFER
2539void kgsl_get_egl_counts(struct kgsl_mem_entry *entry,
2540 int *egl_surface_count, int *egl_image_count)
2541{
2542 struct kgsl_dma_buf_meta *meta = entry->priv_data;
2543 struct dma_buf *dmabuf = meta->dmabuf;
2544 struct dma_buf_attachment *mem_entry_buf_attachment = meta->attach;
2545 struct device *buf_attachment_dev = mem_entry_buf_attachment->dev;
2546 struct dma_buf_attachment *attachment = NULL;
2547
2548 mutex_lock(&dmabuf->lock);
2549 list_for_each_entry(attachment, &dmabuf->attachments, node) {
2550 struct kgsl_mem_entry *scan_mem_entry = NULL;
2551
2552 if (attachment->dev != buf_attachment_dev)
2553 continue;
2554
2555 scan_mem_entry = attachment->priv;
2556 if (!scan_mem_entry)
2557 continue;
2558
2559 switch (kgsl_memdesc_get_memtype(&scan_mem_entry->memdesc)) {
2560 case KGSL_MEMTYPE_EGL_SURFACE:
2561 (*egl_surface_count)++;
2562 break;
2563 case KGSL_MEMTYPE_EGL_IMAGE:
2564 (*egl_image_count)++;
2565 break;
2566 }
2567 }
2568 mutex_unlock(&dmabuf->lock);
2569}
2570#else
2571void kgsl_get_egl_counts(struct kgsl_mem_entry *entry,
2572 int *egl_surface_count, int *egl_image_count)
2573{
2574}
2575#endif
2576
2577long kgsl_ioctl_map_user_mem(struct kgsl_device_private *dev_priv,
2578 unsigned int cmd, void *data)
2579{
2580 int result = -EINVAL;
2581 struct kgsl_map_user_mem *param = data;
2582 struct kgsl_mem_entry *entry = NULL;
2583 struct kgsl_process_private *private = dev_priv->process_priv;
2584 struct kgsl_mmu *mmu = &dev_priv->device->mmu;
2585 unsigned int memtype;
2586
2587 /*
2588 * If content protection is not enabled and secure buffer
2589 * is requested to be mapped return error.
2590 */
2591
2592 if (param->flags & KGSL_MEMFLAGS_SECURE) {
2593 /* Log message and return if context protection isn't enabled */
2594 if (!kgsl_mmu_is_secured(mmu)) {
2595 dev_WARN_ONCE(dev_priv->device->dev, 1,
2596 "Secure buffer not supported");
2597 return -EOPNOTSUPP;
2598 }
2599
2600 /* Can't use CPU map with secure buffers */
2601 if (param->flags & KGSL_MEMFLAGS_USE_CPU_MAP)
2602 return -EINVAL;
2603 }
2604
2605 entry = kgsl_mem_entry_create();
2606
2607 if (entry == NULL)
2608 return -ENOMEM;
2609
2610 /*
2611 * Convert from enum value to KGSL_MEM_ENTRY value, so that
2612 * we can use the latter consistently everywhere.
2613 */
2614 memtype = param->memtype + 1;
2615
2616 /*
2617 * Mask off unknown flags from userspace. This way the caller can
2618 * check if a flag is supported by looking at the returned flags.
2619 * Note: CACHEMODE is ignored for this call. Caching should be
2620 * determined by type of allocation being mapped.
2621 */
2622 param->flags &= KGSL_MEMFLAGS_GPUREADONLY
2623 | KGSL_MEMTYPE_MASK
2624 | KGSL_MEMALIGN_MASK
2625 | KGSL_MEMFLAGS_USE_CPU_MAP
2626 | KGSL_MEMFLAGS_SECURE;
2627 entry->memdesc.flags = ((uint64_t) param->flags)
2628 | KGSL_MEMFLAGS_FORCE_32BIT;
2629
2630 if (!kgsl_mmu_use_cpu_map(mmu))
2631 entry->memdesc.flags &= ~((uint64_t) KGSL_MEMFLAGS_USE_CPU_MAP);
2632
2633 if (MMU_FEATURE(mmu, KGSL_MMU_NEED_GUARD_PAGE))
2634 entry->memdesc.priv |= KGSL_MEMDESC_GUARD_PAGE;
2635
2636 if (param->flags & KGSL_MEMFLAGS_SECURE)
2637 entry->memdesc.priv |= KGSL_MEMDESC_SECURE;
2638
2639 switch (memtype) {
2640 case KGSL_MEM_ENTRY_USER:
2641 result = _map_usermem_addr(dev_priv->device, private->pagetable,
2642 entry, param->hostptr, param->offset, param->len);
2643 break;
2644 case KGSL_MEM_ENTRY_ION:
2645 if (param->offset != 0)
2646 result = -EINVAL;
2647 else
2648 result = _map_usermem_dma_buf(dev_priv->device,
2649 private->pagetable, entry, param->fd);
2650 break;
2651 default:
2652 result = -EOPNOTSUPP;
2653 break;
2654 }
2655
2656 if (result)
2657 goto error;
2658
2659 if ((param->flags & KGSL_MEMFLAGS_SECURE) &&
2660 (entry->memdesc.size & mmu->secure_align_mask)) {
2661 result = -EINVAL;
2662 goto error_attach;
2663 }
2664
2665 if (entry->memdesc.size >= SZ_2M)
2666 kgsl_memdesc_set_align(&entry->memdesc, ilog2(SZ_2M));
2667 else if (entry->memdesc.size >= SZ_1M)
2668 kgsl_memdesc_set_align(&entry->memdesc, ilog2(SZ_1M));
2669 else if (entry->memdesc.size >= SZ_64K)
2670 kgsl_memdesc_set_align(&entry->memdesc, ilog2(SZ_64));
2671
2672 /* echo back flags */
2673 param->flags = (unsigned int) entry->memdesc.flags;
2674
2675 result = kgsl_mem_entry_attach_process(dev_priv->device, private,
2676 entry);
2677 if (result)
2678 goto error_attach;
2679
2680 /* Adjust the returned value for a non 4k aligned offset */
2681 param->gpuaddr = (unsigned long)
2682 entry->memdesc.gpuaddr + (param->offset & PAGE_MASK);
2683
2684 KGSL_STATS_ADD(param->len, &kgsl_driver.stats.mapped,
2685 &kgsl_driver.stats.mapped_max);
2686
2687 kgsl_process_add_stats(private,
2688 kgsl_memdesc_usermem_type(&entry->memdesc), param->len);
2689
2690 trace_kgsl_mem_map(entry, param->fd);
2691
2692 kgsl_mem_entry_commit_process(entry);
2693 return result;
2694
2695error_attach:
2696 switch (memtype) {
2697 case KGSL_MEM_ENTRY_ION:
2698 kgsl_destroy_ion(entry->priv_data);
2699 entry->memdesc.sgt = NULL;
2700 break;
2701 default:
2702 break;
2703 }
2704 kgsl_sharedmem_free(&entry->memdesc);
2705error:
2706 /* Clear gpuaddr here so userspace doesn't get any wrong ideas */
2707 param->gpuaddr = 0;
2708
2709 kfree(entry);
2710 return result;
2711}
2712
2713static int _kgsl_gpumem_sync_cache(struct kgsl_mem_entry *entry,
2714 uint64_t offset, uint64_t length, unsigned int op)
2715{
2716 int ret = 0;
2717 int cacheop;
2718 int mode;
2719
2720 /*
2721 * Flush is defined as (clean | invalidate). If both bits are set, then
2722 * do a flush, otherwise check for the individual bits and clean or inv
2723 * as requested
2724 */
2725
2726 if ((op & KGSL_GPUMEM_CACHE_FLUSH) == KGSL_GPUMEM_CACHE_FLUSH)
2727 cacheop = KGSL_CACHE_OP_FLUSH;
2728 else if (op & KGSL_GPUMEM_CACHE_CLEAN)
2729 cacheop = KGSL_CACHE_OP_CLEAN;
2730 else if (op & KGSL_GPUMEM_CACHE_INV)
2731 cacheop = KGSL_CACHE_OP_INV;
2732 else {
2733 ret = -EINVAL;
2734 goto done;
2735 }
2736
2737 if (!(op & KGSL_GPUMEM_CACHE_RANGE)) {
2738 offset = 0;
2739 length = entry->memdesc.size;
2740 }
2741
2742 mode = kgsl_memdesc_get_cachemode(&entry->memdesc);
2743 if (mode != KGSL_CACHEMODE_UNCACHED
2744 && mode != KGSL_CACHEMODE_WRITECOMBINE) {
2745 trace_kgsl_mem_sync_cache(entry, offset, length, op);
2746 ret = kgsl_cache_range_op(&entry->memdesc, offset,
2747 length, cacheop);
2748 }
2749
2750done:
2751 return ret;
2752}
2753
2754/* New cache sync function - supports both directions (clean and invalidate) */
2755
2756long kgsl_ioctl_gpumem_sync_cache(struct kgsl_device_private *dev_priv,
2757 unsigned int cmd, void *data)
2758{
2759 struct kgsl_gpumem_sync_cache *param = data;
2760 struct kgsl_process_private *private = dev_priv->process_priv;
2761 struct kgsl_mem_entry *entry = NULL;
2762 long ret;
2763
2764 if (param->id != 0)
2765 entry = kgsl_sharedmem_find_id(private, param->id);
2766 else if (param->gpuaddr != 0)
2767 entry = kgsl_sharedmem_find(private, (uint64_t) param->gpuaddr);
2768
2769 if (entry == NULL)
2770 return -EINVAL;
2771
2772 ret = _kgsl_gpumem_sync_cache(entry, (uint64_t) param->offset,
2773 (uint64_t) param->length, param->op);
2774 kgsl_mem_entry_put(entry);
2775 return ret;
2776}
2777
2778static int mem_id_cmp(const void *_a, const void *_b)
2779{
2780 const unsigned int *a = _a, *b = _b;
2781
2782 if (*a == *b)
2783 return 0;
2784 return (*a > *b) ? 1 : -1;
2785}
2786
2787#ifdef CONFIG_ARM64
2788/* Do not support full flush on ARM64 targets */
2789static inline bool check_full_flush(size_t size, int op)
2790{
2791 return false;
2792}
2793#else
2794/* Support full flush if the size is bigger than the threshold */
2795static inline bool check_full_flush(size_t size, int op)
2796{
2797 /* If we exceed the breakeven point, flush the entire cache */
2798 bool ret = (kgsl_driver.full_cache_threshold != 0) &&
2799 (size >= kgsl_driver.full_cache_threshold) &&
2800 (op == KGSL_GPUMEM_CACHE_FLUSH);
2801 if (ret) {
2802 trace_kgsl_mem_sync_full_cache(actual_count, op_size);
2803 flush_cache_all();
2804 }
2805 return ret;
2806}
2807#endif
2808
2809long kgsl_ioctl_gpumem_sync_cache_bulk(struct kgsl_device_private *dev_priv,
2810 unsigned int cmd, void *data)
2811{
2812 int i;
2813 struct kgsl_gpumem_sync_cache_bulk *param = data;
2814 struct kgsl_process_private *private = dev_priv->process_priv;
2815 unsigned int id, last_id = 0, *id_list = NULL, actual_count = 0;
2816 struct kgsl_mem_entry **entries = NULL;
2817 long ret = 0;
2818 uint64_t op_size = 0;
2819 bool full_flush = false;
2820
2821 if (param->id_list == NULL || param->count == 0
2822 || param->count > (PAGE_SIZE / sizeof(unsigned int)))
2823 return -EINVAL;
2824
2825 id_list = kcalloc(param->count, sizeof(unsigned int), GFP_KERNEL);
2826 if (id_list == NULL)
2827 return -ENOMEM;
2828
2829 entries = kcalloc(param->count, sizeof(*entries), GFP_KERNEL);
2830 if (entries == NULL) {
2831 ret = -ENOMEM;
2832 goto end;
2833 }
2834
2835 if (copy_from_user(id_list, param->id_list,
2836 param->count * sizeof(unsigned int))) {
2837 ret = -EFAULT;
2838 goto end;
2839 }
2840 /* sort the ids so we can weed out duplicates */
2841 sort(id_list, param->count, sizeof(*id_list), mem_id_cmp, NULL);
2842
2843 for (i = 0; i < param->count; i++) {
2844 unsigned int cachemode;
2845 struct kgsl_mem_entry *entry = NULL;
2846
2847 id = id_list[i];
2848 /* skip 0 ids or duplicates */
2849 if (id == last_id)
2850 continue;
2851
2852 entry = kgsl_sharedmem_find_id(private, id);
2853 if (entry == NULL)
2854 continue;
2855
2856 /* skip uncached memory */
2857 cachemode = kgsl_memdesc_get_cachemode(&entry->memdesc);
2858 if (cachemode != KGSL_CACHEMODE_WRITETHROUGH &&
2859 cachemode != KGSL_CACHEMODE_WRITEBACK) {
2860 kgsl_mem_entry_put(entry);
2861 continue;
2862 }
2863
2864 op_size += entry->memdesc.size;
2865 entries[actual_count++] = entry;
2866
2867 full_flush = check_full_flush(op_size, param->op);
2868 if (full_flush)
2869 break;
2870
2871 last_id = id;
2872 }
2873
2874 param->op &= ~KGSL_GPUMEM_CACHE_RANGE;
2875
2876 for (i = 0; i < actual_count; i++) {
2877 if (!full_flush)
2878 _kgsl_gpumem_sync_cache(entries[i], 0,
2879 entries[i]->memdesc.size,
2880 param->op);
2881 kgsl_mem_entry_put(entries[i]);
2882 }
2883end:
2884 kfree(entries);
2885 kfree(id_list);
2886 return ret;
2887}
2888
2889/* Legacy cache function, does a flush (clean + invalidate) */
2890
2891long kgsl_ioctl_sharedmem_flush_cache(struct kgsl_device_private *dev_priv,
2892 unsigned int cmd, void *data)
2893{
2894 struct kgsl_sharedmem_free *param = data;
2895 struct kgsl_process_private *private = dev_priv->process_priv;
2896 struct kgsl_mem_entry *entry = NULL;
2897 long ret;
2898
2899 entry = kgsl_sharedmem_find(private, (uint64_t) param->gpuaddr);
2900 if (entry == NULL)
2901 return -EINVAL;
2902
2903 ret = _kgsl_gpumem_sync_cache(entry, 0, entry->memdesc.size,
2904 KGSL_GPUMEM_CACHE_FLUSH);
2905 kgsl_mem_entry_put(entry);
2906 return ret;
2907}
2908
2909long kgsl_ioctl_gpuobj_sync(struct kgsl_device_private *dev_priv,
2910 unsigned int cmd, void *data)
2911{
2912 struct kgsl_process_private *private = dev_priv->process_priv;
2913 struct kgsl_gpuobj_sync *param = data;
2914 struct kgsl_gpuobj_sync_obj *objs;
2915 struct kgsl_mem_entry **entries;
2916 long ret = 0;
2917 bool full_flush = false;
2918 uint64_t size = 0;
2919 int i, count = 0;
2920 void __user *ptr;
2921
2922 if (param->count == 0 || param->count > 128)
2923 return -EINVAL;
2924
2925 objs = kcalloc(param->count, sizeof(*objs), GFP_KERNEL);
2926 if (objs == NULL)
2927 return -ENOMEM;
2928
2929 entries = kcalloc(param->count, sizeof(*entries), GFP_KERNEL);
2930 if (entries == NULL) {
2931 ret = -ENOMEM;
2932 goto out;
2933 }
2934
2935 ptr = to_user_ptr(param->objs);
2936
2937 for (i = 0; i < param->count; i++) {
2938 ret = _copy_from_user(&objs[i], ptr, sizeof(*objs),
2939 param->obj_len);
2940 if (ret)
2941 goto out;
2942
2943 entries[i] = kgsl_sharedmem_find_id(private, objs[i].id);
2944
2945 /* Not finding the ID is not a fatal failure - just skip it */
2946 if (entries[i] == NULL)
2947 continue;
2948
2949 count++;
2950
2951 if (!(objs[i].op & KGSL_GPUMEM_CACHE_RANGE))
2952 size += entries[i]->memdesc.size;
2953 else if (objs[i].offset < entries[i]->memdesc.size)
2954 size += (entries[i]->memdesc.size - objs[i].offset);
2955
2956 full_flush = check_full_flush(size, objs[i].op);
2957 if (full_flush)
2958 break;
2959
2960 ptr += sizeof(*objs);
2961 }
2962
2963 if (!full_flush) {
2964 for (i = 0; !ret && i < param->count; i++)
2965 if (entries[i])
2966 ret = _kgsl_gpumem_sync_cache(entries[i],
2967 objs[i].offset, objs[i].length,
2968 objs[i].op);
2969 }
2970
2971 for (i = 0; i < param->count; i++)
2972 if (entries[i])
2973 kgsl_mem_entry_put(entries[i]);
2974
2975out:
2976 kfree(entries);
2977 kfree(objs);
2978
2979 return ret;
2980}
2981
2982#ifdef CONFIG_ARM64
2983static uint64_t kgsl_filter_cachemode(uint64_t flags)
2984{
2985 /*
2986 * WRITETHROUGH is not supported in arm64, so we tell the user that we
2987 * use WRITEBACK which is the default caching policy.
2988 */
2989 if ((flags & KGSL_CACHEMODE_MASK) >> KGSL_CACHEMODE_SHIFT ==
2990 KGSL_CACHEMODE_WRITETHROUGH) {
2991 flags &= ~((uint64_t) KGSL_CACHEMODE_MASK);
2992 flags |= (KGSL_CACHEMODE_WRITEBACK << KGSL_CACHEMODE_SHIFT) &
2993 KGSL_CACHEMODE_MASK;
2994 }
2995 return flags;
2996}
2997#else
2998static uint64_t kgsl_filter_cachemode(uint64_t flags)
2999{
3000 return flags;
3001}
3002#endif
3003
3004/* The largest allowable alignment for a GPU object is 32MB */
3005#define KGSL_MAX_ALIGN (32 * SZ_1M)
3006
3007static struct kgsl_mem_entry *gpumem_alloc_entry(
3008 struct kgsl_device_private *dev_priv,
3009 uint64_t size, uint64_t flags)
3010{
3011 int ret;
3012 struct kgsl_process_private *private = dev_priv->process_priv;
3013 struct kgsl_mem_entry *entry;
3014 unsigned int align;
3015
3016 flags &= KGSL_MEMFLAGS_GPUREADONLY
3017 | KGSL_CACHEMODE_MASK
3018 | KGSL_MEMTYPE_MASK
3019 | KGSL_MEMALIGN_MASK
3020 | KGSL_MEMFLAGS_USE_CPU_MAP
3021 | KGSL_MEMFLAGS_SECURE
3022 | KGSL_MEMFLAGS_FORCE_32BIT;
3023
3024 /* Turn off SVM if the system doesn't support it */
3025 if (!kgsl_mmu_use_cpu_map(&dev_priv->device->mmu))
3026 flags &= ~((uint64_t) KGSL_MEMFLAGS_USE_CPU_MAP);
3027
3028 /* Return not supported error if secure memory isn't enabled */
3029 if (!kgsl_mmu_is_secured(&dev_priv->device->mmu) &&
3030 (flags & KGSL_MEMFLAGS_SECURE)) {
3031 dev_WARN_ONCE(dev_priv->device->dev, 1,
3032 "Secure memory not supported");
3033 return ERR_PTR(-EOPNOTSUPP);
3034 }
3035
3036 /* Secure memory disables advanced addressing modes */
3037 if (flags & KGSL_MEMFLAGS_SECURE)
3038 flags &= ~((uint64_t) KGSL_MEMFLAGS_USE_CPU_MAP);
3039
3040 /* Cap the alignment bits to the highest number we can handle */
3041 align = MEMFLAGS(flags, KGSL_MEMALIGN_MASK, KGSL_MEMALIGN_SHIFT);
3042 if (align >= ilog2(KGSL_MAX_ALIGN)) {
3043 KGSL_CORE_ERR("Alignment too large; restricting to %dK\n",
3044 KGSL_MAX_ALIGN >> 10);
3045
3046 flags &= ~((uint64_t) KGSL_MEMALIGN_MASK);
3047 flags |= (ilog2(KGSL_MAX_ALIGN) << KGSL_MEMALIGN_SHIFT) &
3048 KGSL_MEMALIGN_MASK;
3049 }
3050
3051 /* For now only allow allocations up to 4G */
3052 if (size == 0 || size > UINT_MAX)
3053 return ERR_PTR(-EINVAL);
3054
3055 flags = kgsl_filter_cachemode(flags);
3056
3057 entry = kgsl_mem_entry_create();
3058 if (entry == NULL)
3059 return ERR_PTR(-ENOMEM);
3060
3061 if (MMU_FEATURE(&dev_priv->device->mmu, KGSL_MMU_NEED_GUARD_PAGE))
3062 entry->memdesc.priv |= KGSL_MEMDESC_GUARD_PAGE;
3063
3064 if (flags & KGSL_MEMFLAGS_SECURE)
3065 entry->memdesc.priv |= KGSL_MEMDESC_SECURE;
3066
3067 ret = kgsl_allocate_user(dev_priv->device, &entry->memdesc,
3068 size, flags);
3069 if (ret != 0)
3070 goto err;
3071
3072 ret = kgsl_mem_entry_attach_process(dev_priv->device, private, entry);
3073 if (ret != 0) {
3074 kgsl_sharedmem_free(&entry->memdesc);
3075 goto err;
3076 }
3077
3078 kgsl_process_add_stats(private,
3079 kgsl_memdesc_usermem_type(&entry->memdesc),
3080 entry->memdesc.size);
3081 trace_kgsl_mem_alloc(entry);
3082
3083 kgsl_mem_entry_commit_process(entry);
3084 return entry;
3085err:
3086 kfree(entry);
3087 return ERR_PTR(ret);
3088}
3089
3090static void copy_metadata(struct kgsl_mem_entry *entry, uint64_t metadata,
3091 unsigned int len)
3092{
3093 unsigned int i, size;
3094
3095 if (len == 0)
3096 return;
3097
3098 size = min_t(unsigned int, len, sizeof(entry->metadata) - 1);
3099
3100 if (copy_from_user(entry->metadata, to_user_ptr(metadata), size)) {
3101 memset(entry->metadata, 0, sizeof(entry->metadata));
3102 return;
3103 }
3104
3105 /* Clean up non printable characters in the string */
3106 for (i = 0; i < size && entry->metadata[i] != 0; i++) {
3107 if (!isprint(entry->metadata[i]))
3108 entry->metadata[i] = '?';
3109 }
3110}
3111
3112long kgsl_ioctl_gpuobj_alloc(struct kgsl_device_private *dev_priv,
3113 unsigned int cmd, void *data)
3114{
3115 struct kgsl_gpuobj_alloc *param = data;
3116 struct kgsl_mem_entry *entry;
3117
3118 entry = gpumem_alloc_entry(dev_priv, param->size, param->flags);
3119
3120 if (IS_ERR(entry))
3121 return PTR_ERR(entry);
3122
3123 copy_metadata(entry, param->metadata, param->metadata_len);
3124
3125 param->size = entry->memdesc.size;
3126 param->flags = entry->memdesc.flags;
3127 param->mmapsize = kgsl_memdesc_footprint(&entry->memdesc);
3128 param->id = entry->id;
3129
3130 return 0;
3131}
3132
3133long kgsl_ioctl_gpumem_alloc(struct kgsl_device_private *dev_priv,
3134 unsigned int cmd, void *data)
3135{
3136 struct kgsl_gpumem_alloc *param = data;
3137 struct kgsl_mem_entry *entry;
3138 uint64_t flags = param->flags;
3139
3140 /* Legacy functions doesn't support these advanced features */
3141 flags &= ~((uint64_t) KGSL_MEMFLAGS_USE_CPU_MAP);
3142 flags |= KGSL_MEMFLAGS_FORCE_32BIT;
3143
3144 entry = gpumem_alloc_entry(dev_priv, (uint64_t) param->size, flags);
3145
3146 if (IS_ERR(entry))
3147 return PTR_ERR(entry);
3148
3149 param->gpuaddr = (unsigned long) entry->memdesc.gpuaddr;
3150 param->size = (size_t) entry->memdesc.size;
3151 param->flags = (unsigned int) entry->memdesc.flags;
3152
3153 return 0;
3154}
3155
3156long kgsl_ioctl_gpumem_alloc_id(struct kgsl_device_private *dev_priv,
3157 unsigned int cmd, void *data)
3158{
3159 struct kgsl_gpumem_alloc_id *param = data;
3160 struct kgsl_mem_entry *entry;
3161 uint64_t flags = param->flags;
3162
3163 flags |= KGSL_MEMFLAGS_FORCE_32BIT;
3164
3165 entry = gpumem_alloc_entry(dev_priv, (uint64_t) param->size, flags);
3166
3167 if (IS_ERR(entry))
3168 return PTR_ERR(entry);
3169
3170 param->id = entry->id;
3171 param->flags = (unsigned int) entry->memdesc.flags;
3172 param->size = (size_t) entry->memdesc.size;
3173 param->mmapsize = (size_t) kgsl_memdesc_footprint(&entry->memdesc);
3174 param->gpuaddr = (unsigned long) entry->memdesc.gpuaddr;
3175
3176 return 0;
3177}
3178
3179long kgsl_ioctl_gpumem_get_info(struct kgsl_device_private *dev_priv,
3180 unsigned int cmd, void *data)
3181{
3182 struct kgsl_process_private *private = dev_priv->process_priv;
3183 struct kgsl_gpumem_get_info *param = data;
3184 struct kgsl_mem_entry *entry = NULL;
3185 int result = 0;
3186
3187 if (param->id != 0)
3188 entry = kgsl_sharedmem_find_id(private, param->id);
3189 else if (param->gpuaddr != 0)
3190 entry = kgsl_sharedmem_find(private, (uint64_t) param->gpuaddr);
3191
3192 if (entry == NULL)
3193 return -EINVAL;
3194
3195 /*
3196 * If any of the 64 bit address / sizes would end up being
3197 * truncated, return -ERANGE. That will signal the user that they
3198 * should use a more modern API
3199 */
3200 if (entry->memdesc.gpuaddr > ULONG_MAX)
3201 result = -ERANGE;
3202
3203 param->gpuaddr = (unsigned long) entry->memdesc.gpuaddr;
3204 param->id = entry->id;
3205 param->flags = (unsigned int) entry->memdesc.flags;
3206 param->size = (size_t) entry->memdesc.size;
3207 param->mmapsize = (size_t) kgsl_memdesc_footprint(&entry->memdesc);
3208 param->useraddr = entry->memdesc.useraddr;
3209
3210 kgsl_mem_entry_put(entry);
3211 return result;
3212}
3213
3214static inline int _sparse_alloc_param_sanity_check(uint64_t size,
3215 uint64_t pagesize)
3216{
3217 if (size == 0 || pagesize == 0)
3218 return -EINVAL;
3219
3220 if (pagesize != PAGE_SIZE && pagesize != SZ_64K)
3221 return -EINVAL;
3222
3223 if (pagesize > size || !IS_ALIGNED(size, pagesize))
3224 return -EINVAL;
3225
3226 return 0;
3227}
3228
3229long kgsl_ioctl_sparse_phys_alloc(struct kgsl_device_private *dev_priv,
3230 unsigned int cmd, void *data)
3231{
3232 struct kgsl_process_private *process = dev_priv->process_priv;
3233 struct kgsl_sparse_phys_alloc *param = data;
3234 struct kgsl_mem_entry *entry;
3235 int ret;
3236 int id;
3237
3238 ret = _sparse_alloc_param_sanity_check(param->size, param->pagesize);
3239 if (ret)
3240 return ret;
3241
3242 entry = kgsl_mem_entry_create();
3243 if (entry == NULL)
3244 return -ENOMEM;
3245
3246 ret = kgsl_process_private_get(process);
3247 if (!ret) {
3248 ret = -EBADF;
3249 goto err_free_entry;
3250 }
3251
3252 idr_preload(GFP_KERNEL);
3253 spin_lock(&process->mem_lock);
3254 /* Allocate the ID but don't attach the pointer just yet */
3255 id = idr_alloc(&process->mem_idr, NULL, 1, 0, GFP_NOWAIT);
3256 spin_unlock(&process->mem_lock);
3257 idr_preload_end();
3258
3259 if (id < 0) {
3260 ret = id;
3261 goto err_put_proc_priv;
3262 }
3263
3264 entry->id = id;
3265 entry->priv = process;
3266
3267 entry->memdesc.flags = KGSL_MEMFLAGS_SPARSE_PHYS;
3268 kgsl_memdesc_set_align(&entry->memdesc, ilog2(param->pagesize));
3269
3270 ret = kgsl_allocate_user(dev_priv->device, &entry->memdesc,
3271 param->size, entry->memdesc.flags);
3272 if (ret)
3273 goto err_remove_idr;
3274
3275 /* Sanity check to verify we got correct pagesize */
3276 if (param->pagesize != PAGE_SIZE && entry->memdesc.sgt != NULL) {
3277 struct scatterlist *s;
3278 int i;
3279
3280 for_each_sg(entry->memdesc.sgt->sgl, s,
3281 entry->memdesc.sgt->nents, i) {
3282 if (!IS_ALIGNED(s->length, param->pagesize))
3283 goto err_invalid_pages;
3284 }
3285 }
3286
3287 param->id = entry->id;
3288 param->flags = entry->memdesc.flags;
3289
3290 trace_sparse_phys_alloc(entry->id, param->size, param->pagesize);
3291 kgsl_mem_entry_commit_process(entry);
3292
3293 return 0;
3294
3295err_invalid_pages:
3296 kgsl_sharedmem_free(&entry->memdesc);
3297err_remove_idr:
3298 spin_lock(&process->mem_lock);
3299 idr_remove(&process->mem_idr, entry->id);
3300 spin_unlock(&process->mem_lock);
3301err_put_proc_priv:
3302 kgsl_process_private_put(process);
3303err_free_entry:
3304 kfree(entry);
3305
3306 return ret;
3307}
3308
3309long kgsl_ioctl_sparse_phys_free(struct kgsl_device_private *dev_priv,
3310 unsigned int cmd, void *data)
3311{
3312 struct kgsl_process_private *process = dev_priv->process_priv;
3313 struct kgsl_sparse_phys_free *param = data;
3314 struct kgsl_mem_entry *entry;
3315
3316 entry = kgsl_sharedmem_find_id_flags(process, param->id,
3317 KGSL_MEMFLAGS_SPARSE_PHYS);
3318 if (entry == NULL)
3319 return -EINVAL;
3320
3321 if (entry->memdesc.cur_bindings != 0) {
3322 kgsl_mem_entry_put(entry);
3323 return -EINVAL;
3324 }
3325
3326 trace_sparse_phys_free(entry->id);
3327
3328 /* One put for find_id(), one put for the kgsl_mem_entry_create() */
3329 kgsl_mem_entry_put(entry);
3330 kgsl_mem_entry_put(entry);
3331
3332 return 0;
3333}
3334
3335long kgsl_ioctl_sparse_virt_alloc(struct kgsl_device_private *dev_priv,
3336 unsigned int cmd, void *data)
3337{
3338 struct kgsl_process_private *private = dev_priv->process_priv;
3339 struct kgsl_sparse_virt_alloc *param = data;
3340 struct kgsl_mem_entry *entry;
3341 int ret;
3342
3343 ret = _sparse_alloc_param_sanity_check(param->size, param->pagesize);
3344 if (ret)
3345 return ret;
3346
3347 entry = kgsl_mem_entry_create();
3348 if (entry == NULL)
3349 return -ENOMEM;
3350
3351 entry->memdesc.flags = KGSL_MEMFLAGS_SPARSE_VIRT;
3352 entry->memdesc.size = param->size;
3353 entry->memdesc.cur_bindings = 0;
3354 kgsl_memdesc_set_align(&entry->memdesc, ilog2(param->pagesize));
3355
3356 spin_lock_init(&entry->bind_lock);
3357 entry->bind_tree = RB_ROOT;
3358
3359 ret = kgsl_mem_entry_attach_process(dev_priv->device, private, entry);
3360 if (ret) {
3361 kfree(entry);
3362 return ret;
3363 }
3364
3365 param->id = entry->id;
3366 param->gpuaddr = entry->memdesc.gpuaddr;
3367 param->flags = entry->memdesc.flags;
3368
3369 trace_sparse_virt_alloc(entry->id, param->size, param->pagesize);
3370 kgsl_mem_entry_commit_process(entry);
3371
3372 return 0;
3373}
3374
3375long kgsl_ioctl_sparse_virt_free(struct kgsl_device_private *dev_priv,
3376 unsigned int cmd, void *data)
3377{
3378 struct kgsl_process_private *process = dev_priv->process_priv;
3379 struct kgsl_sparse_virt_free *param = data;
3380 struct kgsl_mem_entry *entry = NULL;
3381
3382 entry = kgsl_sharedmem_find_id_flags(process, param->id,
3383 KGSL_MEMFLAGS_SPARSE_VIRT);
3384 if (entry == NULL)
3385 return -EINVAL;
3386
3387 if (entry->bind_tree.rb_node != NULL) {
3388 kgsl_mem_entry_put(entry);
3389 return -EINVAL;
3390 }
3391
3392 trace_sparse_virt_free(entry->id);
3393
3394 /* One put for find_id(), one put for the kgsl_mem_entry_create() */
3395 kgsl_mem_entry_put(entry);
3396 kgsl_mem_entry_put(entry);
3397
3398 return 0;
3399}
3400
3401static int _sparse_add_to_bind_tree(struct kgsl_mem_entry *entry,
3402 uint64_t v_offset,
3403 struct kgsl_memdesc *memdesc,
3404 uint64_t p_offset,
3405 uint64_t size,
3406 uint64_t flags)
3407{
3408 struct sparse_bind_object *new;
3409 struct rb_node **node, *parent = NULL;
3410
3411 new = kzalloc(sizeof(*new), GFP_KERNEL);
3412 if (new == NULL)
3413 return -ENOMEM;
3414
3415 new->v_off = v_offset;
3416 new->p_off = p_offset;
3417 new->p_memdesc = memdesc;
3418 new->size = size;
3419 new->flags = flags;
3420
3421 node = &entry->bind_tree.rb_node;
3422
3423 while (*node != NULL) {
3424 struct sparse_bind_object *this;
3425
3426 parent = *node;
3427 this = rb_entry(parent, struct sparse_bind_object, node);
3428
3429 if (new->v_off < this->v_off)
3430 node = &parent->rb_left;
3431 else if (new->v_off > this->v_off)
3432 node = &parent->rb_right;
3433 }
3434
3435 rb_link_node(&new->node, parent, node);
3436 rb_insert_color(&new->node, &entry->bind_tree);
3437
3438 return 0;
3439}
3440
3441static int _sparse_rm_from_bind_tree(struct kgsl_mem_entry *entry,
3442 struct sparse_bind_object *obj,
3443 uint64_t v_offset, uint64_t size)
3444{
3445 spin_lock(&entry->bind_lock);
3446 if (v_offset == obj->v_off && size >= obj->size) {
3447 /*
3448 * We are all encompassing, remove the entry and free
3449 * things up
3450 */
3451 rb_erase(&obj->node, &entry->bind_tree);
3452 kfree(obj);
3453 } else if (v_offset == obj->v_off) {
3454 /*
3455 * We are the front of the node, adjust the front of
3456 * the node
3457 */
3458 obj->v_off += size;
3459 obj->p_off += size;
3460 obj->size -= size;
3461 } else if ((v_offset + size) == (obj->v_off + obj->size)) {
3462 /*
3463 * We are at the end of the obj, adjust the beginning
3464 * points
3465 */
3466 obj->size -= size;
3467 } else {
3468 /*
3469 * We are in the middle of a node, split it up and
3470 * create a new mini node. Adjust this node's bounds
3471 * and add the new node to the list.
3472 */
3473 uint64_t tmp_size = obj->size;
3474 int ret;
3475
3476 obj->size = v_offset - obj->v_off;
3477
3478 spin_unlock(&entry->bind_lock);
3479 ret = _sparse_add_to_bind_tree(entry, v_offset + size,
3480 obj->p_memdesc,
3481 obj->p_off + (v_offset - obj->v_off) + size,
3482 tmp_size - (v_offset - obj->v_off) - size,
3483 obj->flags);
3484
3485 return ret;
3486 }
3487
3488 spin_unlock(&entry->bind_lock);
3489
3490 return 0;
3491}
3492
3493static struct sparse_bind_object *_find_containing_bind_obj(
3494 struct kgsl_mem_entry *entry,
3495 uint64_t offset, uint64_t size)
3496{
3497 struct sparse_bind_object *obj = NULL;
3498 struct rb_node *node = entry->bind_tree.rb_node;
3499
3500 spin_lock(&entry->bind_lock);
3501
3502 while (node != NULL) {
3503 obj = rb_entry(node, struct sparse_bind_object, node);
3504
3505 if (offset == obj->v_off) {
3506 break;
3507 } else if (offset < obj->v_off) {
3508 if (offset + size > obj->v_off)
3509 break;
3510 node = node->rb_left;
3511 obj = NULL;
3512 } else if (offset > obj->v_off) {
3513 if (offset < obj->v_off + obj->size)
3514 break;
3515 node = node->rb_right;
3516 obj = NULL;
3517 }
3518 }
3519
3520 spin_unlock(&entry->bind_lock);
3521
3522 return obj;
3523}
3524
3525static int _sparse_unbind(struct kgsl_mem_entry *entry,
3526 struct sparse_bind_object *bind_obj,
3527 uint64_t offset, uint64_t size)
3528{
3529 struct kgsl_memdesc *memdesc = bind_obj->p_memdesc;
3530 struct kgsl_pagetable *pt = memdesc->pagetable;
3531 int ret;
3532
3533 if (memdesc->cur_bindings < (size / PAGE_SIZE))
3534 return -EINVAL;
3535
3536 memdesc->cur_bindings -= size / PAGE_SIZE;
3537
3538 ret = kgsl_mmu_unmap_offset(pt, memdesc,
3539 entry->memdesc.gpuaddr, offset, size);
3540 if (ret)
3541 return ret;
3542
3543 ret = kgsl_mmu_sparse_dummy_map(pt, &entry->memdesc, offset, size);
3544 if (ret)
3545 return ret;
3546
3547 ret = _sparse_rm_from_bind_tree(entry, bind_obj, offset, size);
3548 if (ret == 0) {
3549 atomic_long_sub(size, &kgsl_driver.stats.mapped);
3550 trace_sparse_unbind(entry->id, offset, size);
3551 }
3552
3553 return ret;
3554}
3555
3556static long sparse_unbind_range(struct kgsl_sparse_binding_object *obj,
3557 struct kgsl_mem_entry *virt_entry)
3558{
3559 struct sparse_bind_object *bind_obj;
3560 int ret = 0;
3561 uint64_t size = obj->size;
3562 uint64_t tmp_size = obj->size;
3563 uint64_t offset = obj->virtoffset;
3564
3565 while (size > 0 && ret == 0) {
3566 tmp_size = size;
3567 bind_obj = _find_containing_bind_obj(virt_entry, offset, size);
3568 if (bind_obj == NULL)
3569 return 0;
3570
3571 if (bind_obj->v_off > offset) {
3572 tmp_size = size - bind_obj->v_off - offset;
3573 if (tmp_size > bind_obj->size)
3574 tmp_size = bind_obj->size;
3575 offset = bind_obj->v_off;
3576 } else if (bind_obj->v_off < offset) {
3577 uint64_t diff = offset - bind_obj->v_off;
3578
3579 if (diff + size > bind_obj->size)
3580 tmp_size = bind_obj->size - diff;
3581 } else {
3582 if (tmp_size > bind_obj->size)
3583 tmp_size = bind_obj->size;
3584 }
3585
3586 ret = _sparse_unbind(virt_entry, bind_obj, offset, tmp_size);
3587 if (ret == 0) {
3588 offset += tmp_size;
3589 size -= tmp_size;
3590 }
3591 }
3592
3593 return ret;
3594}
3595
3596static inline bool _is_phys_bindable(struct kgsl_mem_entry *phys_entry,
3597 uint64_t offset, uint64_t size, uint64_t flags)
3598{
3599 struct kgsl_memdesc *memdesc = &phys_entry->memdesc;
3600
3601 if (!IS_ALIGNED(offset | size, kgsl_memdesc_get_pagesize(memdesc)))
3602 return false;
3603
3604 if (!(flags & KGSL_SPARSE_BIND_MULTIPLE_TO_PHYS) &&
3605 offset + size > memdesc->size)
3606 return false;
3607
3608 return true;
3609}
3610
3611static int _sparse_bind(struct kgsl_process_private *process,
3612 struct kgsl_mem_entry *virt_entry, uint64_t v_offset,
3613 struct kgsl_mem_entry *phys_entry, uint64_t p_offset,
3614 uint64_t size, uint64_t flags)
3615{
3616 int ret;
3617 struct kgsl_pagetable *pagetable;
3618 struct kgsl_memdesc *memdesc = &phys_entry->memdesc;
3619
3620 /* map the memory after unlocking if gpuaddr has been assigned */
3621 if (memdesc->gpuaddr)
3622 return -EINVAL;
3623
3624 if (memdesc->useraddr != 0)
3625 return -EINVAL;
3626
3627 pagetable = memdesc->pagetable;
3628
3629 /* Clear out any mappings */
3630 ret = kgsl_mmu_unmap_offset(pagetable, &virt_entry->memdesc,
3631 virt_entry->memdesc.gpuaddr, v_offset, size);
3632 if (ret)
3633 return ret;
3634
3635 ret = kgsl_mmu_map_offset(pagetable, virt_entry->memdesc.gpuaddr,
3636 v_offset, memdesc, p_offset, size, flags);
3637 if (ret) {
3638 /* Try to clean up, but not the end of the world */
3639 kgsl_mmu_sparse_dummy_map(pagetable, &virt_entry->memdesc,
3640 v_offset, size);
3641 return ret;
3642 }
3643
3644 ret = _sparse_add_to_bind_tree(virt_entry, v_offset, memdesc,
3645 p_offset, size, flags);
3646 if (ret == 0)
3647 memdesc->cur_bindings += size / PAGE_SIZE;
3648
3649 return ret;
3650}
3651
3652static long sparse_bind_range(struct kgsl_process_private *private,
3653 struct kgsl_sparse_binding_object *obj,
3654 struct kgsl_mem_entry *virt_entry)
3655{
3656 struct kgsl_mem_entry *phys_entry;
3657 int ret;
3658
3659 phys_entry = kgsl_sharedmem_find_id_flags(private, obj->id,
3660 KGSL_MEMFLAGS_SPARSE_PHYS);
3661 if (phys_entry == NULL)
3662 return -EINVAL;
3663
3664 if (!_is_phys_bindable(phys_entry, obj->physoffset, obj->size,
3665 obj->flags)) {
3666 kgsl_mem_entry_put(phys_entry);
3667 return -EINVAL;
3668 }
3669
3670 if (kgsl_memdesc_get_align(&virt_entry->memdesc) !=
3671 kgsl_memdesc_get_align(&phys_entry->memdesc)) {
3672 kgsl_mem_entry_put(phys_entry);
3673 return -EINVAL;
3674 }
3675
3676 ret = sparse_unbind_range(obj, virt_entry);
3677 if (ret) {
3678 kgsl_mem_entry_put(phys_entry);
3679 return -EINVAL;
3680 }
3681
3682 ret = _sparse_bind(private, virt_entry, obj->virtoffset,
3683 phys_entry, obj->physoffset, obj->size,
3684 obj->flags & KGSL_SPARSE_BIND_MULTIPLE_TO_PHYS);
3685 if (ret == 0) {
3686 KGSL_STATS_ADD(obj->size, &kgsl_driver.stats.mapped,
3687 &kgsl_driver.stats.mapped_max);
3688
3689 trace_sparse_bind(virt_entry->id, obj->virtoffset,
3690 phys_entry->id, obj->physoffset,
3691 obj->size, obj->flags);
3692 }
3693
3694 kgsl_mem_entry_put(phys_entry);
3695
3696 return ret;
3697}
3698
3699long kgsl_ioctl_sparse_bind(struct kgsl_device_private *dev_priv,
3700 unsigned int cmd, void *data)
3701{
3702 struct kgsl_process_private *private = dev_priv->process_priv;
3703 struct kgsl_sparse_bind *param = data;
3704 struct kgsl_sparse_binding_object obj;
3705 struct kgsl_mem_entry *virt_entry;
3706 int pg_sz;
3707 void __user *ptr;
3708 int ret = 0;
3709 int i = 0;
3710
3711 ptr = (void __user *) (uintptr_t) param->list;
3712
3713 if (param->size > sizeof(struct kgsl_sparse_binding_object) ||
3714 param->count == 0 || ptr == NULL)
3715 return -EINVAL;
3716
3717 virt_entry = kgsl_sharedmem_find_id_flags(private, param->id,
3718 KGSL_MEMFLAGS_SPARSE_VIRT);
3719 if (virt_entry == NULL)
3720 return -EINVAL;
3721
3722 pg_sz = kgsl_memdesc_get_pagesize(&virt_entry->memdesc);
3723
3724 for (i = 0; i < param->count; i++) {
3725 memset(&obj, 0, sizeof(obj));
3726 ret = _copy_from_user(&obj, ptr, sizeof(obj), param->size);
3727 if (ret)
3728 break;
3729
3730 /* Sanity check initial range */
3731 if (obj.size == 0 ||
3732 obj.virtoffset + obj.size > virt_entry->memdesc.size ||
3733 !(IS_ALIGNED(obj.virtoffset | obj.size, pg_sz))) {
3734 ret = -EINVAL;
3735 break;
3736 }
3737
3738 if (obj.flags & KGSL_SPARSE_BIND)
3739 ret = sparse_bind_range(private, &obj, virt_entry);
3740 else if (obj.flags & KGSL_SPARSE_UNBIND)
3741 ret = sparse_unbind_range(&obj, virt_entry);
3742 else
3743 ret = -EINVAL;
3744 if (ret)
3745 break;
3746
3747 ptr += sizeof(obj);
3748 }
3749
3750 kgsl_mem_entry_put(virt_entry);
3751
3752 return ret;
3753}
3754
3755long kgsl_ioctl_gpuobj_info(struct kgsl_device_private *dev_priv,
3756 unsigned int cmd, void *data)
3757{
3758 struct kgsl_process_private *private = dev_priv->process_priv;
3759 struct kgsl_gpuobj_info *param = data;
3760 struct kgsl_mem_entry *entry;
3761
3762 if (param->id == 0)
3763 return -EINVAL;
3764
3765 entry = kgsl_sharedmem_find_id(private, param->id);
3766 if (entry == NULL)
3767 return -EINVAL;
3768
3769 param->id = entry->id;
3770 param->gpuaddr = entry->memdesc.gpuaddr;
3771 param->flags = entry->memdesc.flags;
3772 param->size = entry->memdesc.size;
3773 param->va_len = kgsl_memdesc_footprint(&entry->memdesc);
3774 param->va_addr = (uint64_t) entry->memdesc.useraddr;
3775
3776 kgsl_mem_entry_put(entry);
3777 return 0;
3778}
3779
3780long kgsl_ioctl_gpuobj_set_info(struct kgsl_device_private *dev_priv,
3781 unsigned int cmd, void *data)
3782{
3783 struct kgsl_process_private *private = dev_priv->process_priv;
3784 struct kgsl_gpuobj_set_info *param = data;
3785 struct kgsl_mem_entry *entry;
3786
3787 if (param->id == 0)
3788 return -EINVAL;
3789
3790 entry = kgsl_sharedmem_find_id(private, param->id);
3791 if (entry == NULL)
3792 return -EINVAL;
3793
3794 if (param->flags & KGSL_GPUOBJ_SET_INFO_METADATA)
3795 copy_metadata(entry, param->metadata, param->metadata_len);
3796
3797 if (param->flags & KGSL_GPUOBJ_SET_INFO_TYPE) {
3798 entry->memdesc.flags &= ~((uint64_t) KGSL_MEMTYPE_MASK);
3799 entry->memdesc.flags |= param->type << KGSL_MEMTYPE_SHIFT;
3800 }
3801
3802 kgsl_mem_entry_put(entry);
3803 return 0;
3804}
3805
3806/**
3807 * kgsl_ioctl_timestamp_event - Register a new timestamp event from userspace
3808 * @dev_priv - pointer to the private device structure
3809 * @cmd - the ioctl cmd passed from kgsl_ioctl
3810 * @data - the user data buffer from kgsl_ioctl
3811 * @returns 0 on success or error code on failure
3812 */
3813
3814long kgsl_ioctl_timestamp_event(struct kgsl_device_private *dev_priv,
3815 unsigned int cmd, void *data)
3816{
3817 struct kgsl_timestamp_event *param = data;
3818 int ret;
3819
3820 switch (param->type) {
3821 case KGSL_TIMESTAMP_EVENT_FENCE:
3822 ret = kgsl_add_fence_event(dev_priv->device,
3823 param->context_id, param->timestamp, param->priv,
3824 param->len, dev_priv);
3825 break;
3826 default:
3827 ret = -EINVAL;
3828 }
3829
3830 return ret;
3831}
3832
3833static int
3834kgsl_mmap_memstore(struct kgsl_device *device, struct vm_area_struct *vma)
3835{
3836 struct kgsl_memdesc *memdesc = &device->memstore;
3837 int result;
3838 unsigned int vma_size = vma->vm_end - vma->vm_start;
3839
3840 /* The memstore can only be mapped as read only */
3841
3842 if (vma->vm_flags & VM_WRITE)
3843 return -EPERM;
3844
3845 if (memdesc->size != vma_size) {
3846 KGSL_MEM_ERR(device, "memstore bad size: %d should be %llu\n",
3847 vma_size, memdesc->size);
3848 return -EINVAL;
3849 }
3850
3851 vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
3852
3853 result = remap_pfn_range(vma, vma->vm_start,
3854 device->memstore.physaddr >> PAGE_SHIFT,
3855 vma_size, vma->vm_page_prot);
3856 if (result != 0)
3857 KGSL_MEM_ERR(device, "remap_pfn_range failed: %d\n",
3858 result);
3859
3860 return result;
3861}
3862
3863/*
3864 * kgsl_gpumem_vm_open is called whenever a vma region is copied or split.
3865 * Increase the refcount to make sure that the accounting stays correct
3866 */
3867
3868static void kgsl_gpumem_vm_open(struct vm_area_struct *vma)
3869{
3870 struct kgsl_mem_entry *entry = vma->vm_private_data;
3871
3872 if (kgsl_mem_entry_get(entry) == 0)
3873 vma->vm_private_data = NULL;
3874}
3875
3876static int
3877kgsl_gpumem_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
3878{
3879 struct kgsl_mem_entry *entry = vma->vm_private_data;
3880
3881 if (!entry)
3882 return VM_FAULT_SIGBUS;
3883 if (!entry->memdesc.ops || !entry->memdesc.ops->vmfault)
3884 return VM_FAULT_SIGBUS;
3885
3886 return entry->memdesc.ops->vmfault(&entry->memdesc, vma, vmf);
3887}
3888
3889static void
3890kgsl_gpumem_vm_close(struct vm_area_struct *vma)
3891{
3892 struct kgsl_mem_entry *entry = vma->vm_private_data;
3893
3894 if (!entry)
3895 return;
3896
3897 entry->memdesc.useraddr = 0;
3898 kgsl_mem_entry_put(entry);
3899}
3900
3901static const struct vm_operations_struct kgsl_gpumem_vm_ops = {
3902 .open = kgsl_gpumem_vm_open,
3903 .fault = kgsl_gpumem_vm_fault,
3904 .close = kgsl_gpumem_vm_close,
3905};
3906
3907static int
3908get_mmap_entry(struct kgsl_process_private *private,
3909 struct kgsl_mem_entry **out_entry, unsigned long pgoff,
3910 unsigned long len)
3911{
3912 int ret = 0;
3913 struct kgsl_mem_entry *entry;
3914
3915 entry = kgsl_sharedmem_find_id(private, pgoff);
3916 if (entry == NULL)
3917 entry = kgsl_sharedmem_find(private, pgoff << PAGE_SHIFT);
3918
3919 if (!entry)
3920 return -EINVAL;
3921
3922 if (!entry->memdesc.ops ||
3923 !entry->memdesc.ops->vmflags ||
3924 !entry->memdesc.ops->vmfault) {
3925 ret = -EINVAL;
3926 goto err_put;
3927 }
3928
3929 if (entry->memdesc.flags & KGSL_MEMFLAGS_SPARSE_PHYS) {
3930 if (len != entry->memdesc.size) {
3931 ret = -EINVAL;
3932 goto err_put;
3933 }
3934 }
3935
3936 if (entry->memdesc.useraddr != 0) {
3937 ret = -EBUSY;
3938 goto err_put;
3939 }
3940
3941 if (kgsl_memdesc_use_cpu_map(&entry->memdesc)) {
3942 if (len != kgsl_memdesc_footprint(&entry->memdesc)) {
3943 ret = -ERANGE;
3944 goto err_put;
3945 }
3946 } else if (len != kgsl_memdesc_footprint(&entry->memdesc) &&
3947 len != entry->memdesc.size) {
3948 /*
3949 * If cpu_map != gpumap then user can map either the
3950 * footprint or the entry size
3951 */
3952 ret = -ERANGE;
3953 goto err_put;
3954 }
3955
3956 *out_entry = entry;
3957 return 0;
3958err_put:
3959 kgsl_mem_entry_put(entry);
3960 return ret;
3961}
3962
3963static unsigned long _gpu_set_svm_region(struct kgsl_process_private *private,
3964 struct kgsl_mem_entry *entry, unsigned long addr,
3965 unsigned long size)
3966{
3967 int ret;
3968
3969 ret = kgsl_mmu_set_svm_region(private->pagetable, (uint64_t) addr,
3970 (uint64_t) size);
3971
3972 if (ret != 0)
3973 return ret;
3974
3975 entry->memdesc.gpuaddr = (uint64_t) addr;
3976 entry->memdesc.pagetable = private->pagetable;
3977
3978 ret = kgsl_mmu_map(private->pagetable, &entry->memdesc);
3979 if (ret) {
3980 kgsl_mmu_put_gpuaddr(&entry->memdesc);
3981 return ret;
3982 }
3983
3984 kgsl_memfree_purge(private->pagetable, entry->memdesc.gpuaddr,
3985 entry->memdesc.size);
3986
3987 return addr;
3988}
3989
3990static unsigned long _gpu_find_svm(struct kgsl_process_private *private,
3991 unsigned long start, unsigned long end, unsigned long len,
3992 unsigned int align)
3993{
3994 uint64_t addr = kgsl_mmu_find_svm_region(private->pagetable,
3995 (uint64_t) start, (uint64_t)end, (uint64_t) len, align);
3996
3997 BUG_ON(!IS_ERR_VALUE((unsigned long)addr) && (addr > ULONG_MAX));
3998
3999 return (unsigned long) addr;
4000}
4001
4002/* Search top down in the CPU VM region for a free address */
4003static unsigned long _cpu_get_unmapped_area(unsigned long bottom,
4004 unsigned long top, unsigned long len, unsigned long align)
4005{
4006 struct vm_unmapped_area_info info;
4007 unsigned long addr, err;
4008
4009 info.flags = VM_UNMAPPED_AREA_TOPDOWN;
4010 info.low_limit = bottom;
4011 info.high_limit = top;
4012 info.length = len;
4013 info.align_offset = 0;
4014 info.align_mask = align - 1;
4015
4016 addr = vm_unmapped_area(&info);
4017
4018 if (IS_ERR_VALUE(addr))
4019 return addr;
4020
4021 err = security_mmap_addr(addr);
4022 return err ? err : addr;
4023}
4024
4025static unsigned long _search_range(struct kgsl_process_private *private,
4026 struct kgsl_mem_entry *entry,
4027 unsigned long start, unsigned long end,
4028 unsigned long len, uint64_t align)
4029{
4030 unsigned long cpu, gpu = end, result = -ENOMEM;
4031
4032 while (gpu > start) {
4033 /* find a new empty spot on the CPU below the last one */
4034 cpu = _cpu_get_unmapped_area(start, gpu, len,
4035 (unsigned long) align);
4036 if (IS_ERR_VALUE(cpu)) {
4037 result = cpu;
4038 break;
4039 }
4040 /* try to map it on the GPU */
4041 result = _gpu_set_svm_region(private, entry, cpu, len);
4042 if (!IS_ERR_VALUE(result))
4043 break;
4044
4045 trace_kgsl_mem_unmapped_area_collision(entry, cpu, len);
4046
4047 if (cpu <= start) {
4048 result = -ENOMEM;
4049 break;
4050 }
4051
4052 /* move downward to the next empty spot on the GPU */
4053 gpu = _gpu_find_svm(private, start, cpu, len, align);
4054 if (IS_ERR_VALUE(gpu)) {
4055 result = gpu;
4056 break;
4057 }
4058
4059 /* Check that_gpu_find_svm doesn't put us in a loop */
4060 if (gpu >= cpu) {
4061 result = -ENOMEM;
4062 break;
4063 }
4064
4065 /* Break if the recommended GPU address is out of range */
4066 if (gpu < start) {
4067 result = -ENOMEM;
4068 break;
4069 }
4070
4071 /*
4072 * Add the length of the chunk to the GPU address to yield the
4073 * upper bound for the CPU search
4074 */
4075 gpu += len;
4076 }
4077 return result;
4078}
4079
4080static unsigned long _get_svm_area(struct kgsl_process_private *private,
4081 struct kgsl_mem_entry *entry, unsigned long hint,
4082 unsigned long len, unsigned long flags)
4083{
4084 uint64_t start, end;
4085 int align_shift = kgsl_memdesc_get_align(&entry->memdesc);
4086 uint64_t align;
4087 unsigned long result;
4088 unsigned long addr;
4089
4090 if (align_shift >= ilog2(SZ_2M))
4091 align = SZ_2M;
4092 else if (align_shift >= ilog2(SZ_1M))
4093 align = SZ_1M;
4094 else if (align_shift >= ilog2(SZ_64K))
4095 align = SZ_64K;
4096 else
4097 align = SZ_4K;
4098
4099 /* get the GPU pagetable's SVM range */
4100 if (kgsl_mmu_svm_range(private->pagetable, &start, &end,
4101 entry->memdesc.flags))
4102 return -ERANGE;
4103
4104 /* now clamp the range based on the CPU's requirements */
4105 start = max_t(uint64_t, start, mmap_min_addr);
4106 end = min_t(uint64_t, end, current->mm->mmap_base);
4107 if (start >= end)
4108 return -ERANGE;
4109
4110 if (flags & MAP_FIXED) {
4111 /* we must use addr 'hint' or fail */
4112 return _gpu_set_svm_region(private, entry, hint, len);
4113 } else if (hint != 0) {
4114 struct vm_area_struct *vma;
4115
4116 /*
4117 * See if the hint is usable, if not we will use
4118 * it as the start point for searching.
4119 */
4120 addr = clamp_t(unsigned long, hint & ~(align - 1),
4121 start, (end - len) & ~(align - 1));
4122
4123 vma = find_vma(current->mm, addr);
4124
4125 if (vma == NULL || ((addr + len) <= vma->vm_start)) {
4126 result = _gpu_set_svm_region(private, entry, addr, len);
4127
4128 /* On failure drop down to keep searching */
4129 if (!IS_ERR_VALUE(result))
4130 return result;
4131 }
4132 } else {
4133 /* no hint, start search at the top and work down */
4134 addr = end & ~(align - 1);
4135 }
4136
4137 /*
4138 * Search downwards from the hint first. If that fails we
4139 * must try to search above it.
4140 */
4141 result = _search_range(private, entry, start, addr, len, align);
4142 if (IS_ERR_VALUE(result) && hint != 0)
4143 result = _search_range(private, entry, addr, end, len, align);
4144
4145 return result;
4146}
4147
4148static unsigned long
4149kgsl_get_unmapped_area(struct file *file, unsigned long addr,
4150 unsigned long len, unsigned long pgoff,
4151 unsigned long flags)
4152{
4153 unsigned long val;
4154 unsigned long vma_offset = pgoff << PAGE_SHIFT;
4155 struct kgsl_device_private *dev_priv = file->private_data;
4156 struct kgsl_process_private *private = dev_priv->process_priv;
4157 struct kgsl_device *device = dev_priv->device;
4158 struct kgsl_mem_entry *entry = NULL;
4159
4160 if (vma_offset == (unsigned long) device->memstore.gpuaddr)
4161 return get_unmapped_area(NULL, addr, len, pgoff, flags);
4162
4163 val = get_mmap_entry(private, &entry, pgoff, len);
4164 if (val)
4165 return val;
4166
4167 /* Do not allow CPU mappings for secure buffers */
4168 if (kgsl_memdesc_is_secured(&entry->memdesc)) {
4169 val = -EPERM;
4170 goto put;
4171 }
4172
4173 if (!kgsl_memdesc_use_cpu_map(&entry->memdesc)) {
4174 val = get_unmapped_area(NULL, addr, len, 0, flags);
4175 if (IS_ERR_VALUE(val))
4176 KGSL_MEM_ERR(device,
4177 "get_unmapped_area: pid %d addr %lx pgoff %lx len %ld failed error %d\n",
4178 private->pid, addr, pgoff, len, (int) val);
4179 } else {
4180 val = _get_svm_area(private, entry, addr, len, flags);
4181 if (IS_ERR_VALUE(val))
4182 KGSL_MEM_ERR(device,
4183 "_get_svm_area: pid %d addr %lx pgoff %lx len %ld failed error %d\n",
4184 private->pid, addr, pgoff, len, (int) val);
4185 }
4186
4187put:
4188 kgsl_mem_entry_put(entry);
4189 return val;
4190}
4191
4192static int kgsl_mmap(struct file *file, struct vm_area_struct *vma)
4193{
4194 unsigned int ret, cache;
4195 unsigned long vma_offset = vma->vm_pgoff << PAGE_SHIFT;
4196 struct kgsl_device_private *dev_priv = file->private_data;
4197 struct kgsl_process_private *private = dev_priv->process_priv;
4198 struct kgsl_mem_entry *entry = NULL;
4199 struct kgsl_device *device = dev_priv->device;
4200
4201 /* Handle leagacy behavior for memstore */
4202
4203 if (vma_offset == (unsigned long) device->memstore.gpuaddr)
4204 return kgsl_mmap_memstore(device, vma);
4205
4206 /*
4207 * The reference count on the entry that we get from
4208 * get_mmap_entry() will be held until kgsl_gpumem_vm_close().
4209 */
4210 ret = get_mmap_entry(private, &entry, vma->vm_pgoff,
4211 vma->vm_end - vma->vm_start);
4212 if (ret)
4213 return ret;
4214
4215 vma->vm_flags |= entry->memdesc.ops->vmflags;
4216
4217 vma->vm_private_data = entry;
4218
4219 /* Determine user-side caching policy */
4220
4221 cache = kgsl_memdesc_get_cachemode(&entry->memdesc);
4222
4223 switch (cache) {
4224 case KGSL_CACHEMODE_UNCACHED:
4225 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
4226 break;
4227 case KGSL_CACHEMODE_WRITETHROUGH:
4228 vma->vm_page_prot = pgprot_writethroughcache(vma->vm_page_prot);
4229 if (pgprot_val(vma->vm_page_prot) ==
4230 pgprot_val(pgprot_writebackcache(vma->vm_page_prot)))
4231 WARN_ONCE(1, "WRITETHROUGH is deprecated for arm64");
4232 break;
4233 case KGSL_CACHEMODE_WRITEBACK:
4234 vma->vm_page_prot = pgprot_writebackcache(vma->vm_page_prot);
4235 break;
4236 case KGSL_CACHEMODE_WRITECOMBINE:
4237 default:
4238 vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
4239 break;
4240 }
4241
4242 vma->vm_ops = &kgsl_gpumem_vm_ops;
4243
4244 if (cache == KGSL_CACHEMODE_WRITEBACK
4245 || cache == KGSL_CACHEMODE_WRITETHROUGH) {
4246 int i;
4247 unsigned long addr = vma->vm_start;
4248 struct kgsl_memdesc *m = &entry->memdesc;
4249
4250 for (i = 0; i < m->page_count; i++) {
4251 struct page *page = m->pages[i];
4252
4253 vm_insert_page(vma, addr, page);
4254 addr += PAGE_SIZE;
4255 }
4256 }
4257
4258 vma->vm_file = file;
4259
4260 entry->memdesc.useraddr = vma->vm_start;
4261
4262 trace_kgsl_mem_mmap(entry);
4263 return 0;
4264}
4265
4266static irqreturn_t kgsl_irq_handler(int irq, void *data)
4267{
4268 struct kgsl_device *device = data;
4269
4270 return device->ftbl->irq_handler(device);
4271
4272}
4273
4274#define KGSL_READ_MESSAGE "OH HAI GPU\n"
4275
4276static ssize_t kgsl_read(struct file *filep, char __user *buf, size_t count,
4277 loff_t *pos)
4278{
4279 return simple_read_from_buffer(buf, count, pos,
4280 KGSL_READ_MESSAGE, strlen(KGSL_READ_MESSAGE) + 1);
4281}
4282
4283static const struct file_operations kgsl_fops = {
4284 .owner = THIS_MODULE,
4285 .release = kgsl_release,
4286 .open = kgsl_open,
4287 .mmap = kgsl_mmap,
4288 .read = kgsl_read,
4289 .get_unmapped_area = kgsl_get_unmapped_area,
4290 .unlocked_ioctl = kgsl_ioctl,
4291 .compat_ioctl = kgsl_compat_ioctl,
4292};
4293
4294struct kgsl_driver kgsl_driver = {
4295 .process_mutex = __MUTEX_INITIALIZER(kgsl_driver.process_mutex),
4296 .ptlock = __SPIN_LOCK_UNLOCKED(kgsl_driver.ptlock),
4297 .devlock = __MUTEX_INITIALIZER(kgsl_driver.devlock),
4298 /*
4299 * Full cache flushes are faster than line by line on at least
4300 * 8064 and 8974 once the region to be flushed is > 16mb.
4301 */
4302 .full_cache_threshold = SZ_16M,
4303
4304 .stats.vmalloc = ATOMIC_LONG_INIT(0),
4305 .stats.vmalloc_max = ATOMIC_LONG_INIT(0),
4306 .stats.page_alloc = ATOMIC_LONG_INIT(0),
4307 .stats.page_alloc_max = ATOMIC_LONG_INIT(0),
4308 .stats.coherent = ATOMIC_LONG_INIT(0),
4309 .stats.coherent_max = ATOMIC_LONG_INIT(0),
4310 .stats.secure = ATOMIC_LONG_INIT(0),
4311 .stats.secure_max = ATOMIC_LONG_INIT(0),
4312 .stats.mapped = ATOMIC_LONG_INIT(0),
4313 .stats.mapped_max = ATOMIC_LONG_INIT(0),
4314};
4315EXPORT_SYMBOL(kgsl_driver);
4316
4317static void _unregister_device(struct kgsl_device *device)
4318{
4319 int minor;
4320
4321 mutex_lock(&kgsl_driver.devlock);
4322 for (minor = 0; minor < KGSL_DEVICE_MAX; minor++) {
4323 if (device == kgsl_driver.devp[minor])
4324 break;
4325 }
4326 if (minor != KGSL_DEVICE_MAX) {
4327 device_destroy(kgsl_driver.class,
4328 MKDEV(MAJOR(kgsl_driver.major), minor));
4329 kgsl_driver.devp[minor] = NULL;
4330 }
4331 mutex_unlock(&kgsl_driver.devlock);
4332}
4333
4334static int _register_device(struct kgsl_device *device)
4335{
4336 int minor, ret;
4337 dev_t dev;
4338
4339 /* Find a minor for the device */
4340
4341 mutex_lock(&kgsl_driver.devlock);
4342 for (minor = 0; minor < KGSL_DEVICE_MAX; minor++) {
4343 if (kgsl_driver.devp[minor] == NULL) {
4344 kgsl_driver.devp[minor] = device;
4345 break;
4346 }
4347 }
4348 mutex_unlock(&kgsl_driver.devlock);
4349
4350 if (minor == KGSL_DEVICE_MAX) {
4351 KGSL_CORE_ERR("minor devices exhausted\n");
4352 return -ENODEV;
4353 }
4354
4355 /* Create the device */
4356 dev = MKDEV(MAJOR(kgsl_driver.major), minor);
4357 device->dev = device_create(kgsl_driver.class,
4358 &device->pdev->dev,
4359 dev, device,
4360 device->name);
4361
4362 if (IS_ERR(device->dev)) {
4363 mutex_lock(&kgsl_driver.devlock);
4364 kgsl_driver.devp[minor] = NULL;
4365 mutex_unlock(&kgsl_driver.devlock);
4366 ret = PTR_ERR(device->dev);
4367 KGSL_CORE_ERR("device_create(%s): %d\n", device->name, ret);
4368 return ret;
4369 }
4370
4371 dev_set_drvdata(&device->pdev->dev, device);
4372 return 0;
4373}
4374
4375int kgsl_device_platform_probe(struct kgsl_device *device)
4376{
4377 int status = -EINVAL;
4378 struct resource *res;
4379 int cpu;
4380
4381 status = _register_device(device);
4382 if (status)
4383 return status;
4384
4385 /* Initialize logging first, so that failures below actually print. */
4386 kgsl_device_debugfs_init(device);
4387
4388 status = kgsl_pwrctrl_init(device);
4389 if (status)
4390 goto error;
4391
4392 /* Get starting physical address of device registers */
4393 res = platform_get_resource_byname(device->pdev, IORESOURCE_MEM,
4394 device->iomemname);
4395 if (res == NULL) {
4396 KGSL_DRV_ERR(device, "platform_get_resource_byname failed\n");
4397 status = -EINVAL;
4398 goto error_pwrctrl_close;
4399 }
4400 if (res->start == 0 || resource_size(res) == 0) {
4401 KGSL_DRV_ERR(device, "dev %d invalid register region\n",
4402 device->id);
4403 status = -EINVAL;
4404 goto error_pwrctrl_close;
4405 }
4406
4407 device->reg_phys = res->start;
4408 device->reg_len = resource_size(res);
4409
4410 /*
4411 * Check if a shadermemname is defined, and then get shader memory
4412 * details including shader memory starting physical address
4413 * and shader memory length
4414 */
4415 if (device->shadermemname != NULL) {
4416 res = platform_get_resource_byname(device->pdev, IORESOURCE_MEM,
4417 device->shadermemname);
4418
4419 if (res == NULL) {
4420 KGSL_DRV_WARN(device,
4421 "Shader memory: platform_get_resource_byname failed\n");
4422 }
4423
4424 else {
4425 device->shader_mem_phys = res->start;
4426 device->shader_mem_len = resource_size(res);
4427 }
4428
4429 if (!devm_request_mem_region(device->dev,
4430 device->shader_mem_phys,
4431 device->shader_mem_len,
4432 device->name)) {
4433 KGSL_DRV_WARN(device, "request_mem_region_failed\n");
4434 }
4435 }
4436
4437 if (!devm_request_mem_region(device->dev, device->reg_phys,
4438 device->reg_len, device->name)) {
4439 KGSL_DRV_ERR(device, "request_mem_region failed\n");
4440 status = -ENODEV;
4441 goto error_pwrctrl_close;
4442 }
4443
4444 device->reg_virt = devm_ioremap(device->dev, device->reg_phys,
4445 device->reg_len);
4446
4447 if (device->reg_virt == NULL) {
4448 KGSL_DRV_ERR(device, "ioremap failed\n");
4449 status = -ENODEV;
4450 goto error_pwrctrl_close;
4451 }
4452 /*acquire interrupt */
4453 device->pwrctrl.interrupt_num =
4454 platform_get_irq_byname(device->pdev, device->pwrctrl.irq_name);
4455
4456 if (device->pwrctrl.interrupt_num <= 0) {
4457 KGSL_DRV_ERR(device, "platform_get_irq_byname failed: %d\n",
4458 device->pwrctrl.interrupt_num);
4459 status = -EINVAL;
4460 goto error_pwrctrl_close;
4461 }
4462
4463 status = devm_request_irq(device->dev, device->pwrctrl.interrupt_num,
4464 kgsl_irq_handler, IRQF_TRIGGER_HIGH,
4465 device->name, device);
4466 if (status) {
4467 KGSL_DRV_ERR(device, "request_irq(%d) failed: %d\n",
4468 device->pwrctrl.interrupt_num, status);
4469 goto error_pwrctrl_close;
4470 }
4471 disable_irq(device->pwrctrl.interrupt_num);
4472
4473 KGSL_DRV_INFO(device,
4474 "dev_id %d regs phys 0x%08lx size 0x%08x\n",
4475 device->id, device->reg_phys, device->reg_len);
4476
4477 rwlock_init(&device->context_lock);
4478
4479 setup_timer(&device->idle_timer, kgsl_timer, (unsigned long) device);
4480
4481 status = kgsl_mmu_probe(device, kgsl_mmu_type);
4482 if (status != 0)
4483 goto error_pwrctrl_close;
4484
4485 /* Check to see if our device can perform DMA correctly */
4486 status = dma_set_coherent_mask(&device->pdev->dev, KGSL_DMA_BIT_MASK);
4487 if (status)
4488 goto error_close_mmu;
4489
4490 /* Initialize the memory pools */
4491 kgsl_init_page_pools(device->pdev);
4492
4493 status = kgsl_allocate_global(device, &device->memstore,
4494 KGSL_MEMSTORE_SIZE, 0, KGSL_MEMDESC_CONTIG, "memstore");
4495
4496 if (status != 0)
4497 goto error_close_mmu;
4498
4499 status = kgsl_allocate_global(device, &device->scratch,
4500 PAGE_SIZE, 0, 0, "scratch");
4501 if (status != 0)
4502 goto error_free_memstore;
4503
4504 /*
4505 * The default request type PM_QOS_REQ_ALL_CORES is
4506 * applicable to all CPU cores that are online and
4507 * would have a power impact when there are more
4508 * number of CPUs. PM_QOS_REQ_AFFINE_IRQ request
4509 * type shall update/apply the vote only to that CPU to
4510 * which IRQ's affinity is set to.
4511 */
4512#ifdef CONFIG_SMP
4513
4514 device->pwrctrl.pm_qos_req_dma.type = PM_QOS_REQ_AFFINE_IRQ;
4515 device->pwrctrl.pm_qos_req_dma.irq = device->pwrctrl.interrupt_num;
4516
4517#endif
4518 pm_qos_add_request(&device->pwrctrl.pm_qos_req_dma,
4519 PM_QOS_CPU_DMA_LATENCY,
4520 PM_QOS_DEFAULT_VALUE);
4521
4522 if (device->pwrctrl.l2pc_cpus_mask) {
4523
4524 device->pwrctrl.l2pc_cpus_qos.type =
4525 PM_QOS_REQ_AFFINE_CORES;
4526 cpumask_empty(&device->pwrctrl.l2pc_cpus_qos.cpus_affine);
4527 for_each_possible_cpu(cpu) {
4528 if ((1 << cpu) & device->pwrctrl.l2pc_cpus_mask)
4529 cpumask_set_cpu(cpu, &device->pwrctrl.
4530 l2pc_cpus_qos.cpus_affine);
4531 }
4532
4533 pm_qos_add_request(&device->pwrctrl.l2pc_cpus_qos,
4534 PM_QOS_CPU_DMA_LATENCY,
4535 PM_QOS_DEFAULT_VALUE);
4536 }
4537
4538 device->events_wq = alloc_workqueue("kgsl-events",
4539 WQ_UNBOUND | WQ_MEM_RECLAIM | WQ_SYSFS, 0);
4540
4541 /* Initialize the snapshot engine */
4542 kgsl_device_snapshot_init(device);
4543
4544 /* Initialize common sysfs entries */
4545 kgsl_pwrctrl_init_sysfs(device);
4546
4547 return 0;
4548
4549error_free_memstore:
4550 kgsl_free_global(device, &device->memstore);
4551error_close_mmu:
4552 kgsl_mmu_close(device);
4553error_pwrctrl_close:
4554 kgsl_pwrctrl_close(device);
4555error:
4556 _unregister_device(device);
4557 return status;
4558}
4559EXPORT_SYMBOL(kgsl_device_platform_probe);
4560
4561void kgsl_device_platform_remove(struct kgsl_device *device)
4562{
4563 destroy_workqueue(device->events_wq);
4564
4565 kgsl_device_snapshot_close(device);
4566
4567 kgsl_exit_page_pools();
4568
4569 kgsl_pwrctrl_uninit_sysfs(device);
4570
4571 pm_qos_remove_request(&device->pwrctrl.pm_qos_req_dma);
4572 if (device->pwrctrl.l2pc_cpus_mask)
4573 pm_qos_remove_request(&device->pwrctrl.l2pc_cpus_qos);
4574
4575 idr_destroy(&device->context_idr);
4576
4577 kgsl_free_global(device, &device->scratch);
4578
4579 kgsl_free_global(device, &device->memstore);
4580
4581 kgsl_mmu_close(device);
4582
4583 kgsl_pwrctrl_close(device);
4584
4585 _unregister_device(device);
4586}
4587EXPORT_SYMBOL(kgsl_device_platform_remove);
4588
4589static void kgsl_core_exit(void)
4590{
4591 kgsl_events_exit();
4592 kgsl_core_debugfs_close();
4593
4594 /*
4595 * We call kgsl_sharedmem_uninit_sysfs() and device_unregister()
4596 * only if kgsl_driver.virtdev has been populated.
4597 * We check at least one member of kgsl_driver.virtdev to
4598 * see if it is not NULL (and thus, has been populated).
4599 */
4600 if (kgsl_driver.virtdev.class) {
4601 kgsl_sharedmem_uninit_sysfs();
4602 device_unregister(&kgsl_driver.virtdev);
4603 }
4604
4605 if (kgsl_driver.class) {
4606 class_destroy(kgsl_driver.class);
4607 kgsl_driver.class = NULL;
4608 }
4609
4610 kgsl_drawobj_exit();
4611
4612 kgsl_memfree_exit();
4613 unregister_chrdev_region(kgsl_driver.major, KGSL_DEVICE_MAX);
4614}
4615
4616static int __init kgsl_core_init(void)
4617{
4618 int result = 0;
4619 /* alloc major and minor device numbers */
4620 result = alloc_chrdev_region(&kgsl_driver.major, 0, KGSL_DEVICE_MAX,
4621 "kgsl");
4622
4623 if (result < 0) {
4624
4625 KGSL_CORE_ERR("alloc_chrdev_region failed err = %d\n", result);
4626 goto err;
4627 }
4628
4629 cdev_init(&kgsl_driver.cdev, &kgsl_fops);
4630 kgsl_driver.cdev.owner = THIS_MODULE;
4631 kgsl_driver.cdev.ops = &kgsl_fops;
4632 result = cdev_add(&kgsl_driver.cdev, MKDEV(MAJOR(kgsl_driver.major), 0),
4633 KGSL_DEVICE_MAX);
4634
4635 if (result) {
4636 KGSL_CORE_ERR("kgsl: cdev_add() failed, dev_num= %d, result= %d\n",
4637 kgsl_driver.major, result);
4638 goto err;
4639 }
4640
4641 kgsl_driver.class = class_create(THIS_MODULE, "kgsl");
4642
4643 if (IS_ERR(kgsl_driver.class)) {
4644 result = PTR_ERR(kgsl_driver.class);
4645 KGSL_CORE_ERR("failed to create class for kgsl");
4646 goto err;
4647 }
4648
4649 /*
4650 * Make a virtual device for managing core related things
4651 * in sysfs
4652 */
4653 kgsl_driver.virtdev.class = kgsl_driver.class;
4654 dev_set_name(&kgsl_driver.virtdev, "kgsl");
4655 result = device_register(&kgsl_driver.virtdev);
4656 if (result) {
4657 KGSL_CORE_ERR("driver_register failed\n");
4658 goto err;
4659 }
4660
4661 /* Make kobjects in the virtual device for storing statistics */
4662
4663 kgsl_driver.ptkobj =
4664 kobject_create_and_add("pagetables",
4665 &kgsl_driver.virtdev.kobj);
4666
4667 kgsl_driver.prockobj =
4668 kobject_create_and_add("proc",
4669 &kgsl_driver.virtdev.kobj);
4670
4671 kgsl_core_debugfs_init();
4672
4673 kgsl_sharedmem_init_sysfs();
4674
4675 INIT_LIST_HEAD(&kgsl_driver.process_list);
4676
4677 INIT_LIST_HEAD(&kgsl_driver.pagetable_list);
4678
4679 kgsl_driver.workqueue = alloc_workqueue("kgsl-workqueue",
4680 WQ_UNBOUND | WQ_MEM_RECLAIM | WQ_SYSFS, 0);
4681
4682 kgsl_driver.mem_workqueue = alloc_workqueue("kgsl-mementry",
4683 WQ_UNBOUND | WQ_MEM_RECLAIM, 0);
4684
4685 kgsl_events_init();
4686
4687 result = kgsl_drawobj_init();
4688 if (result)
4689 goto err;
4690
4691 kgsl_memfree_init();
4692
4693 return 0;
4694
4695err:
4696 kgsl_core_exit();
4697 return result;
4698}
4699
4700module_init(kgsl_core_init);
4701module_exit(kgsl_core_exit);
4702
4703MODULE_DESCRIPTION("MSM GPU driver");
4704MODULE_LICENSE("GPL");