ion: remove use of qcom,msm-ion-reserve

The device tree string "qcom,msm-ion-reserve" is no
longer needed since we don't support carveout heaps any more.

Change-Id: Icb24d86fff9b478504d17518fda4c2e7b04833b8
Signed-off-by: Larry Bassel <lbassel@codeaurora.org>
ion: Squashed commit of upstream updates

ion: Add missing unlock on error path

A mutex_unlock was missing for the alloc_lock on an error
path. Add it.

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

ion: fix crash when alloc len is -1

If userspace passes a length between -4095 and -1 to allocate it
will pass the len != 0 check, but when len is page aligned it will
be 0.  Check len after page aligning.

Drop the warning as well, userspace shouldn't be able to trigger
a warning in the kernel.

Change-Id: I96c7142637638991f3a9af9be7cfbb50f79f3803
Signed-off-by: Colin Cross <ccross@android.com>
Git-commit: 49bdc418a3c1129d49fe92d89cf77111584fb51f
Git-repo: http://android.googlesource.com/kernel/common/
Signed-off-by: Mitchel Humpherys <mitchelh@codeaurora.org>

ion: fix dma APIs

__dma_page_cpu_to_dev is a private ARM api that is not available
on 3.10 and was never available on other architectures.  We can
get the same behavior by calling dma_sync_sg_for_device with a
scatterlist containing a single page.  It's still not quite a
kosher use of the dma apis, we still conflate physical addresses
with bus addresses, but it should at least compile on all
platforms, and work on any platform that doesn't have a physical
to bus address translation.

Change-Id: I8451c2dae4bf85841015c016640684ac28430a5a
Signed-off-by: Colin Cross <ccross@android.com>
Git-commit: 51e3580bf9da57da9c860330293b73b0dbb296e0
Git-repo: http://android.googlesource.com/kernel/common/
[mitchelh@codeaurora.org: conflicts due to different cache APIs being
 used. Earlier we changed __dma_page_cpu_to_dev to
 arm_dma_ops.sync_single_for_device so there was a conflict at that call
 site.]
Signed-off-by: Mitchel Humpherys <mitchelh@codeaurora.org>

gpu: ion: Fix performance issue in faulting code

Previously the code to fault ion buffers in one page at a time had a
performance problem caused by the requirement to traverse the sg list
looking for the right page to load in (a result of the fact that the items in
the list may not be of uniform size).  To fix the problem, for buffers
that will be faulted in, also keep a flat array of all the pages in the buffer
to use from the fault handler.  To recover some of the additional memory
footprint this creates per buffer, dirty bits used to indicate which
pages have been faulted in to the cpu are now stored in the low bit of each
page struct pointer in the page array.

Change-Id: I891b077dc0c88ed6d416b256626d8778fd67be84
Signed-off-by: Rebecca Schultz Zavin <rebecca@android.com>
Git-commit: bbf5538ee0e4c30ba9b4556f9699ed5f31df67da
Git-repo: https://android.googlesource.com/kernel/common/
[mitchelh@codeaurora.org: conflicts due to msm modifications. Changed a
 private dma API (__dma_page_cpu_to_dev) to public (ARM-specific)
 wrapper (arm_dma_ops.sync_single_for_device).]
Signed-off-by: Mitchel Humpherys <mitchelh@codeaurora.org>

ion: convert sg_dma_len(sg) to sg->length

ion is always dealing with the allocation and not the mapping,
so it should always be using sg->length and not sg->dma_length.

Change-Id: Id9b07f1196b2bafe04636fa1aa46dfc84d003cf0
Signed-off-by: Colin Cross <ccross@android.com>
Git-commit: c1d3cea1d0b985a78a87675a07c2dab865a416f1
Git-repo: http://android.googlesource.com/kernel/common/
Signed-off-by: Mitchel Humpherys <mitchelh@codeaurora.org>

ion: check invalid values in ion_system_heap

ion_system_heap can only satisfy page alignment, and
ion_system_contig_heap can only satisify alignment to the
allocation size.  Neither can support faulting user mappings
because they use slab pages.

Change-Id: I895c2d4184c672f647f83a17aeb862985dc92f2c
Signed-off-by: Colin Cross <ccross@android.com>
Git-commit: e8f7f42cdcbe3ae4c4bf719847369ddac44219a9
Git-repo: http://android.googlesource.com/kernel/common/
[mitchelh@codeaurora.org: some context due to msm fixes]
Signed-off-by: Mitchel Humpherys <mitchelh@codeaurora.org>

ion: add test device for unit tests to interact with dma_bufs

Add a /dev/ion-test device that will be created if CONFIG_ION_TEST
is set.  The device accepts a dma_buf fd and allows reading and
writing to the backing memory using DMA-like apis or kernel mapping
apis.  Can be used to test the dma_buf mapping ops, including
the ion implementations, from userspace.

Change-Id: I30703ba69cd75bdfe7767ac642e5f0cacd8d0478
Signed-off-by: Colin Cross <ccross@android.com>
Git-commit: 5c31b87cef4e03eea5b417a6e3f7c9b781aacf96
Git-repo: http://android.googlesource.com/kernel/common/
Signed-off-by: Mitchel Humpherys <mitchelh@codeaurora.org>

gpu: ion: ion_chunk_heap: Zero chunk heap memory at creation time

Allocations from the ion heap need to be zeroed to protect userspace
from seeing memory belonging to other processes.  First allocations
from this heap were not zero'd allowing users to see memory from other
processes on a warm reset.

Change-Id: I524a7b79cb76c390c870fcf8b30d213185fc85a0
Signed-off-by: Rebecca Schultz Zavin <rebecca@android.com>
Git-commit: 34e159dd120ce57b9dbfdaecb5904c2be3000a26
Git-repo: http://android.googlesource.com/kernel/common/
Signed-off-by: Mitchel Humpherys <mitchelh@codeaurora.org>

ion: don't use phys_to_page or __phys_to_pfn

phys_to_page and __phys_to_pfn don't exist on all platforms.
Use a combination of pfn_to_page, PFN_DOWN, page_to_pfn, and
virt_to_page to get the same results.

Change-Id: I53cef26059800bc8b7fb85ae458741574c97c257
Signed-off-by: Colin Cross <ccross@android.com>
Git-commit: f26a6f2aa0beea7eeb0a2f69d44a5975ef8c887e
Git-repo: http://android.googlesource.com/kernel/common/
[mitchelh@codeaurora.org: we weren't using the APIs in question in the
 same locations. Also applied the change to some other locations]
Signed-off-by: Mitchel Humpherys <mitchelh@codeaurora.org>

ion: add free list size to heap debug files

Change-Id: I3c6309afdbd661a2f870fd1ba3fea9543e229882
Signed-off-by: Colin Cross <ccross@android.com>
Git-commit: a3041cac9fc117df010e66ce40091fa2c4f646f8
Git-repo: https://android.googlesource.com/kernel/common/
Signed-off-by: Mitchel Humpherys <mitchelh@codeaurora.org>

ion: fix printk warnings

Use %z for size_t and %pa for dma_addr_t to avoid warnings in printks.

Change-Id: I2c72874acd0b69cb35fca691928783817deb9394
Signed-off-by: Colin Cross <ccross@android.com>
Git-commit: 21e08b49ac311e3d1e9c7168de1bd42868176398
Git-repo: http://android.googlesource.com/kernel/common/
[mitchelh@codeaurora.org: we already had most of these right, so this
 turned out to be pretty minimal compared to the original patch.]
Signed-off-by: Mitchel Humpherys <mitchelh@codeaurora.org>

ion: Fix two small issues in system_heap allocation

In testing ion system heap allocations, I ran across two issues:

1) Not k*z*allocing the sg table. This can cause trouble if
we end up trying call sg_alloc_table() with too many entries,
then sg_alloc_table() internally fails and tries to free what it
thinks is internal table structure, which causes bad pointer
traversals.

2) The second list_for_each_entry probably should be _safe,
since I was seeing  strange lock warnings and oopses on occasion.
This seems to resolve it, but could use some extra checking.

Change-Id: I59d4c90104a8cf23dc4ae814d0b17348f1b68ac0
Signed-off-by: John Stultz <john.stultz@linaro.org>
Git-commit: 818017a0caa6b7b3bb6e96697a2c35806e08f6a4
Git-repo: http://android.googlesource.com/kernel/common/
[mitchelh@codeaurora.org: we already had list_for_each_entry_safe from
the commit (fb98460facd6: "gpu: ion: Use list_for_each_safe on error
path")]
Signed-off-by: Mitchel Humpherys <mitchelh@codeaurora.org>

ion: free low memory from page pools first

When the shrinkers are called with GFP_HIGH free low memory first,
it is more important to have free than high memory.

Change-Id: I7ad8a9c133830f04d429c3d87b781b3e862ccedb
Signed-off-by: Colin Cross <ccross@android.com>
Git-commit: b928ad649cd32b4ca5ba7429bfe897433d37dfea
Git-repo: http://android.googlesource.com/kernel/common/
Signed-off-by: Mitchel Humpherys <mitchelh@codeaurora.org>

ion: check return value from remap_pfn_range

Check the return value of remap_pfn_range and return an error if
it fails.

Change-Id: I206cf95a24607ebe1c80274e3ed15cc7c076d007
Signed-off-by: Colin Cross <ccross@android.com>
Git-commit: 3b8da00c6c5e42af7b532dbf51b2d5007095d844
Git-repo: http://android.googlesource.com/kernel/common/
Signed-off-by: Mitchel Humpherys <mitchelh@codeaurora.org>

ion: use vm_insert_pfn for faulted pages

Most ion userspace mappings are created with remap_pfn_range.  Use
vm_insert_pfn instead of vm_insert_page to make faulted cached
mappings look more like uncached mappings.

Change-Id: I9ec5cad3fef54f3b80be8b306d7ff2f1fe3f0e66
Signed-off-by: Colin Cross <ccross@android.com>
Git-commit: ed6cfafce575cc6baa099212c69b56c0132f598d
Git-repo: http://android.googlesource.com/kernel/common/
Signed-off-by: Mitchel Humpherys <mitchelh@codeaurora.org>

ion: remove ion_heap_alloc_pages

Now that ion_vm_fault doesn't need a struct page with a nonzero
refcount, there is no need allocate heap memory for cached pages using
split_page.  Remove the ion_heap_alloc_pages and ion_heap_free_pages
helpers in favor of direct calls to alloc_pages and __free_pages,
and remove the special handling in the system heap.

Change-Id: I5966a798f48df2d56642e662a69c1495944f6509
Signed-off-by: Colin Cross <ccross@android.com>
Git-commit: d671290eae60f4a77ab2aef79cfaed008ee0716d
Git-repo: http://android.googlesource.com/kernel/common/
[mitchelh@codeaurora.org: conflicts since we have page pooling for both
 cached and uncached]
Signed-off-by: Mitchel Humpherys <mitchelh@codeaurora.org>

ion: allow cached mappings of chunk and system heap buffers

Now that ion_vm_fault uses vm_insert_pfn instead of vm_insert_page
cached buffers can be supported in any heap.  Remove the checks
in the chunk and system heaps.

Change-Id: I371a44c400ed8a342c3b0eed90d0fb7060537697
Signed-off-by: Colin Cross <ccross@android.com>
Git-commit: 1f67f7520303192e46e1621639097007ab076b90
Git-repo: http://android.googlesource.com/kernel/common/
[mitchelh@codeaurora.org: context differences due to prior msm related
 to flushing the cache at allocation time. The prior msm fixes are now
 no longer relevant so an additional structure was removed.]
Signed-off-by: Mitchel Humpherys <mitchelh@codeaurora.org>

ion: fix sparse warnings

Fix sparse warnings in ion.

Change-Id: Icbadf2ca53bea20914f608f619568629c178eae3
Signed-off-by: Colin Cross <ccross@android.com>
Git-commit: 6db8a1c0c9817d6ebd61c47110554aa34729e9a7
Git-repo: http://android.googlesource.com/kernel/common/
[mitchelh@codeaurora.org: conflicts since we have page pooling for both
 cached and uncached as well as some gfp flag differences]
Signed-off-by: Mitchel Humpherys <mitchelh@codeaurora.org>

ion: add alignment check to chunk heap

Change-Id: I4be12b9545a81f9b46339a905f00e1e64896b3ed
Signed-off-by: Colin Cross <ccross@android.com>
Git-commit: 1b1cbed2f4cd92b9ccfd28b985983e2d217ecd2e
Git-repo: http://android.googlesource.com/kernel/common/
Signed-off-by: Mitchel Humpherys <mitchelh@codeaurora.org>

ion: Cleanup whitespace issues and other checkpatch problems

Just some simple cleanups to address whitespace issues and
other issues found w/ checkpatch.

Change-Id: I181444505627894b8f3bbf59192703b0f65736ee
Signed-off-by: John Stultz <john.stultz@linaro.org>
Git-commit: 0f9ae3f7fc853efb3f36e44b1c24ca15490e54f0
Git-repo: http://android.googlesource.com/kernel/common/
Signed-off-by: Mitchel Humpherys <mitchelh@codeaurora.org>

ion: remove support for securing entire heaps

Securing an entire heap is no longer required or implemented by any
heaps. Remove the (now unused) upper-level hooks for securing heaps.

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

ion: remove obsolete stub

ion_mark_dangling_buffers_locked is no longer used anywhere. The main
implementation of the function was removed but the stub wasn't. Remove
the stub.

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

ion: remove calls to outer cache routines

There aren't any platforms supported on 3.10 which
have outer caches.

Change-Id: Ia624de9835dd372dcd403fa321e21fc8272cb4bd
Signed-off-by: Larry Bassel <lbassel@codeaurora.org>

gpu: ion: Add trace events for secure cma

Secure CMA allocates and stores memory differently than regular
heaps. This means the regular trace events for tracking allocation
time may not tell the whole story. Add secure CMA specific trace
events for events such as prefetching and shrinking to better collect
data.

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

ion: index client->handles rbtree by buffer

The only remaining users of the client->handles rbtree are
iterating through it like a list.  Keep the rbtree, but change
its index to be the buffer address instead of the handle address,
which makes ion_handle_lookup a fast rbtree search.

Change-Id: Ie7d974b3a5d9831c0d664de85ddae8db3c3abdf9
Signed-off-by: Colin Cross <ccross@android.com>
Git-commit: 002119eeb221cf1efd7afda836da768ff1cd0ad9
Git-repo: http://android.googlesource.com/kernel/common/
Signed-off-by: Mitchel Humpherys <mitchelh@codeaurora.org>

ion: do not use sg_dma_len() macro

Use of sg_dma_len() is not portable to 64 bit and is not used any more
by ion or iommu. Replace the usages by direct access of the length
structure member.

Change-Id: I6c18b2a77e288be312448c36f8ed3ad6db90ac0f
Signed-off-by: Larry Bassel <lbassel@codeaurora.org>

ion: convert map_kernel to return ERR_PTR

ion is going to stop accepting NULL as an error value, use ERR_PTR.

Change-Id: I030e8b72138904e38a4a5d225beaaa98427651fb
Signed-off-by: Colin Cross <ccross@android.com>
Git-commit: d37aa151f874563a35d49a0ff0a129ef332c5e9e
Git-repo: http://android.googlesource.com/kernel/common/
[mitchelh@codeaurora.org: conflicts due to msm changes]
Signed-off-by: Mitchel Humpherys <mitchelh@codeaurora.org>

gpu: ion: delete ion_system_mapper.c

The mapper abstraction layer was removed before the initial ion
commit, but a stray ion_system_mapper.c file was left in.  Delete
it.

Change-Id: I0001d1ea1b866ebd7ec1306e94472c67a094855f
Signed-off-by: Colin Cross <ccross@android.com>
Git-commit: 3922298f52d193d5e014bf4d28e423cf7bd0b50e
Git-repo: http://android.googlesource.com/kernel/common/
Signed-off-by: Mitchel Humpherys <mitchelh@codeaurora.org>

ion: change ion_user_handle_t definition to int

Turn ion_user_handle_t to int. This change reflects the underlying type
returned by the ion driver.

Change-Id: I40390dae8138327769510525bf62e55877a4b37d
Signed-off-by: Rom Lemarchand <romlem@google.com>
Git-commit: dcd2da8015c7ac2da6f57fc0d8c15b80e44c4aed
Git-repo: http://android.googlesource.com/kernel/common/
[mitchelh@codeaurora.org: made changes to file in different location
 since we've already done the uapi split. Also, one function name was
 different due to some necessary patch re-ordering.]
Signed-off-by: Mitchel Humpherys <mitchelh@codeaurora.org>
staging: ion: msm: Only parse available dt nodes

ION is currently ignoring the status property of device tree
heap nodes. Make ION aware of the status of the ION heap nodes making
it possible to disable certain heaps.

Change-Id: I96e6af97df6bbd1c24ace9aa476baae28409ea96
Signed-off-by: Olav Haugan <ohaugan@codeaurora.org>
ion: secure_buffer: fix iteration of sg_table

We're currently re-using the `i' loop counter to iterate over the chunk
list within our iteration of the secure sg_table. This causes the
iteration of the sg_table to get all messed up and results in crashes
when we walk off the end of the list. Fix it.

Change-Id: I09c84015f8c3cbcb2cea60886241457bb829003c
Signed-off-by: Mitchel Humpherys <mitchelh@codeaurora.org>
ion: fix remaining instances of sg_dma_len

Recently, [ec6c8a8f443: "ion: convert sg_dma_len(sg) to sg->length"]
moved Ion away from using sg_dma_len. However, a few instances were left
over from MSM-specific code. Fix these instances so that we're using
sg->length everywhere.

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