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