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