msm: kgsl: Fix searching of memory object

Make sure that at least a size of 1 byte is searched when locating
the memory entry of a region. If size is 0 then a memory region
whose last address is equal to the start address of the memory being
searched will be returned which is wrong.

Change-Id: I643185d1fdd17296bd70fea483aa3c365e691bc5
Signed-off-by: Shubhraprakash Das <sadas@codeaurora.org>
msm: kgsl: If adreno start fails then restore state of device

Restore the state of the device back to what it was at the
start of the adreno_start function if this function fails to
execute successfully.

Change-Id: I5b279e5186b164d3361fba7c8f8d864395b794c8
Signed-off-by: Shubhraprakash Das <sadas@codeaurora.org>
msm: kgsl: Fix early exit condition in ringbuffer drain

The ringbuffer drain function can be called when the ringbuffer
start flag is not set. This happens on startup. Hence,
exiting the function early based on start flag is incorrect.
Simply execute this function regardless of the start flag.

Change-Id: Ibf2075847f8bb1a760bc1550309efb3c7aa1ca49
Signed-off-by: Shubhraprakash Das <sadas@codeaurora.org>
msm: kgsl: Do not return an error on NULL gpu address

If a NULL gpu address is passed to snapshot object tracking
function then do not treat this as an error and return 0. NULL
objects may be present in an IB so just skip over these objects
instead of exiting due to an error.

Signed-off-by: Shubhraprakash Das <sadas@codeaurora.org>
Change-Id: Ic253722c58b41f41d03f83c77017e58365da01a7
msm: kgsl: Don't hold process list global mutex in process private create

Don't hold process list global mutex for long. Instead make
use of process specific spin_lock() to serialize access
to process private structure while creating it. Holding
process list global mutex could lead to deadlocks as other
functions depend on it.

CRs-fixed: 480732
Change-Id: Id54316770f911d0e23384f54ba5c14a1c9113680
Signed-off-by: Harsh Vardhan Dwivedi <hdwivedi@codeaurora.org>
Signed-off-by: Shubhraprakash Das <sadas@codeaurora.org>
msm: kgsl: Use CPU path to program pagetable when active count is 0

When active count is 0 then we should use the CPU path to program
pagetables because the GPU path requires event registration. Events
can only be queued when active count is valid. Hence, if the active
count is NULL then use the CPU path.

Change-Id: I70f5894d20796bdc0f592db7dc2731195c0f7a82
CRs-fixed: 481887
Signed-off-by: Shubhrapralash Das <sadas@codeaurora.org>
msm: kgsl: better handling of virtual address fragmentation

When KGSL_MEMFLAGS_USE_CPU_MAP is enabled, the mmap address
must try to match the GPU alignment requirements of the buffer,
as well as include space in the mapping for the guard page.
This can cause -ENOMEM to be returned from get_unmapped_area()
when there are a large number of mappings. When this happens,
fall back to page alignment and retry to avoid failure.

Change-Id: I2176fe57afc96d8cf1fe1c694836305ddc3c3420
Signed-off-by: Jeremy Gebben <jgebben@codeaurora.org>
msm: kgsl: prevent race between mmap() and free on timestamp

When KGSL_MEMFLAGS_USE_CPU_MAP is set, we must check that the
address from get_unmapped_area() is not used as part of a
mapping that is present only in the GPU pagetable and not the
CPU pagetable. These mappings can occur because when a buffer
is freed on timestamp, the CPU mapping is destroyed immediately
but the GPU mapping is not destroyed until the GPU timestamp
has passed.

Because kgsl_mem_entry_detach_process() removed the rbtree
entry before removing the iommu mapping, there was a window
of time where kgsl thought the address was available even
though it was still present in the iommu pagetable. This
could cause the address to get assigned to a new buffer,
which would cause iommu_map_range() to fail since the old
mapping was still in the pagetable. Prevent this race by
removing the iommu mapping before removing the rbtree entry
tracking the address.

Change-Id: I8f42d6d97833293b55fcbc272d180564862cef8a
CRs-Fixed: 480222
Signed-off-by: Jeremy Gebben <jgebben@codeaurora.org>
msm: kgsl: add guard page support for imported memory

Imported memory buffers sometimes do not have enough
padding to prevent page faults due to overzealous
GPU prefetch. Attach guard pages to their mappings
to prevent these faults.

Because we don't create the scatterlist for some
types of imported memory, such as ion, the guard
page is no longer included as the last entry in
the scatterlist. Instead, it is handled by
size ajustments and a separate iommu_map() call
in the kgsl_mmu_map() and kgsl_mmu_unmap() paths.

Change-Id: I3af3c29c3983f8cacdc366a2423f90c8ecdc3059
Signed-off-by: Jeremy Gebben <jgebben@codeaurora.org>
msm: kgsl: fix kgsl_mem_entry refcounting

Make kgsl_sharedmem_find* return a reference to the
entry that was found. This makes using an entry
without the mem_lock held less race prone.

Change-Id: If6eb6470ecfea1332d3130d877922c70ca037467
Signed-off-by: Jeremy Gebben <jgebben@codeaurora.org>