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