gpu: ion: Add pooling of CMA allocations

CMA provides excellent memory utilization but at the cost of
being much slower than permanently reserving the memory. Most
clients of this memory end up needing to do allocations on the
fast path. These clients may be aware of allocations on the slow
path but may not be able to actually make those allocations until
the fast path. Add support for pooling CMA allocations and allowing
prefetching into the heap. This memory may be stored until either
allocated/freed or drained via a shrinker.

Change-Id: I2f15bc9c72e5f79ffa6edbeb454fd41121efd8fc
Signed-off-by: Laura Abbott <lauraa@codeaurora.org>
gpu: ion: Add ioctls for prefetching/draining pools

Multiple heaps in Ion are begining to pool large amounts of memory.
This gives a boost on average but clients may still take a hit
if the pool needs to be filled up again. Add an ioctl to give a
'hint' to add memory to the pool. Equivalently, add an ioctl to
drain some of the memory out of the pool to ennsure the memory
isn't hanging around for too long.

Change-Id: I2974322caa1520fd199d9cf2658f2fafceffb65c
Signed-off-by: Laura Abbott <lauraa@codeaurora.org>
gpu: ion: Add support for heap walking

Clients may wish to implement custom functions on a particular
heap ID. Add a generic interface to walk all the heaps available
and call a function on the particular heap ID.

Change-Id: I8b3de7a2de74cd9ea1873020ed00ee40100ce923
Signed-off-by: Laura Abbott <lauraa@codeaurora.org>

Conflicts:
	drivers/gpu/ion/ion_priv.h
gpu: ion: Add support for getting the heap size of CMA regions

Currently, Ion heaps are able to allocate memory from appropriate
CMA regions without knowing the maximum size of the CMA region. New
Ion algorithms necessitate having the size of the heap available
for accounting. Add code to parse the devicetree to get the size of
the CMA region and associate it with the appropriate heap.

Change-Id: Ia9b560d496c76209c6116fa00f24f7b624c954f3
Signed-off-by: Laura Abbott <lauraa@codeaurora.org>
gpu: ion: Add support for specifying a default prefetch size

Some heaps now support prefetching memory before allocation occurs.
This currently requires a size to be given to prefetch a given size.
Clients may not know what size they need to prefetch for a given
heap. Since the optimal size may change with the target, add an option
into devicetree to specify what size should be prefetched by default.
Clients may specify a size of 0 to prefetch a the default size.

Change-Id: If15e4051d53fe284129c309101c41ee423f890f9
Signed-off-by: Laura Abbott <lauraa@codeaurora.org>

Conflicts:
	drivers/gpu/ion/msm/msm_ion.c
gpu: ion: Don't recursively take secure chunk lock

During allocation, the chunk_lock is taken to protect the chunk
list. dma_alloc_coherent may invoke the shrinker which also tries
to take the chunk_lock. This leads to a deadlock. If the mutex is
already locked in the shrinker, just return -1 as specifed by the
shrinker API to signal this condition.

Change-Id: I5525ecfcdc6deea61024f0008c62026103728571
Signed-off-by: Laura Abbott <lauraa@codeaurora.org>
ion: replace userspace handle cookies with idr

Userspace handles should not leak kernel virtual addresses to
userspace.  They have to be validated by looking them up in an
rbtree anyways, so replace them with an idr and validate them
by using idr_find to convert the id number to the struct
ion_handle pointer.

Change-Id: Iab5667ba6f3a73256fec3949b23a9a6f8e14a283
Signed-off-by: Colin Cross <ccross@android.com>
Git-commit: 1126d98d8e34d771afcc72ce5d1c37ad4d474402
Git-repo: https://android.googlesource.com/kernel/common
[mitchelh@codeaurora.org: conflicts due to msm changes]
Signed-off-by: Mitchel Humpherys <mitchelh@codeaurora.org>
ion: don't use id 0 for handle cookie

ion userspace clients think that the cookie is a pointer, so they
use NULL to check if the handle has been initialized.  Set the first
id number to 1.

Change-Id: Ifb9af6029a8b08f57e41bc6160cc11e11001a2a9
Signed-off-by: Colin Cross <ccross@android.com>
Git-commit: 66df93b4c9e78cf433b76d7fe3da4cc7fd06c36b
Git-repo: https://android.googlesource.com/kernel/common
Signed-off-by: Mitchel Humpherys <mitchelh@codeaurora.org>
ion: add new ion_user_handle_t type for the user-space token

Declare new ion_user_handle_t type to contain the token returned to user-space.
This allows a 2-step migration of the user-space code to a new kernel header
first, then will allow us to change the definition of the ion_user_handle_type_t
to int without breaking the API.

Change-Id: I4200b6600df8e56fe98cf1580a13a65cb25ec646
Signed-off-by: Rom Lemarchand <romlem@google.com>
(cherry picked from commit ebb8269bbb05b06ecedca3e21b3e65f23d48eadd)
Git-commit: 29f271de6ffdf6a96375cd244986617e87afc8d0
Git-repo: https://android.googlesource.com/kernel/common
Signed-off-by: Mitchel Humpherys <mitchelh@codeaurora.org>
ion: hold reference to handle after ion_uhandle_get

commit 1262ab1846cf76f7549c66ef709120dbfbe6d49f (ion: replace
userspace handle cookies with idr) broke the locking in ion.
The ION_IOC_FREE and ION_IOC_MAP ioctls were relying on
ion_handle_validate to detect the case where a call raced
with another ION_IOC_FREE which may have freed the struct
ion_handle.

Rename ion_uhandle_get to ion_handle_get_by_id, and have it
take the client lock and return with an extra reference to
the handle.  Make each caller put its reference once it
is done with the handle.

Also modify users of ion_handle_validate to continue to hold
the client lock after calling ion_handle_validate until
they are done with the handle, and warn if ion_handle_validate
is called without the client lock held.

Change-Id: I56da5624fca3bed4ee24806b6ec39de903543341
Signed-off-by: Colin Cross <ccross@android.com>
Git-commit: 33a57aa073dac709bcdcba23bc4e2e7fcc6330f6
Git-repo: https://android.googlesource.com/kernel/common
[mitchelh@codeaurora.org: minor merge conflicts due to some missing
 casts since we haven't brought in the change that makes
 ion_user_handle_t an int.]
Signed-off-by: Mitchel Humpherys <mitchelh@codeaurora.org>
ion: export a few handle-related APIs

There are a few APIs (ion_handle_put and ion_handle_get_by_id) that are
useful for implementing Ion extensions (e.g. custom ioctl
handlers). Remove the `static' to make these APIs available to other Ion
code.

Change-Id: Ice8767853f628d8178e96a36db3da02fa97f3235
Signed-off-by: Mitchel Humpherys <mitchelh@codeaurora.org>
ion: msm: update custom caching ioctls for new handle format

The userspace API for Ion handles has changed slightly. Update the MSM
caching ioctls to work with the new format.

Change-Id: I6dee3190f5cad117a4d7059f1c7f0f01e05692c0
Signed-off-by: Mitchel Humpherys <mitchelh@codeaurora.org>
ion: msm: fix bug with handle id handling

0 is not a valid handle id value. If we get a 0 we should try to do the
dma_buf import.

Change-Id: I2b9355a5ad65c91247c0369047f921cf02908b1c
Signed-off-by: Mitchel Humpherys <mitchelh@codeaurora.org>