msm: kgsl: Add draw workaround commands after every 50 context switches

We need to add a draw workaround command sequence after every 50 context
switches if the gmem has not been saved in those 50 switches. This
workaround is required to prevent the GPU from deadlocking. Since gmem
save commands have draw calls in them we only need to execute this
workaround if the gmem has not been saved. The number 50 was chosen
empirically.

CRs-fixed: 362302
Change-Id: I1a659c85736c47fe6f45030b20ce7a12851b0815
Signed-off-by: Shubhraprakash Das <sadas@codeaurora.org>

Conflicts:

	drivers/gpu/msm/adreno.h
	drivers/gpu/msm/adreno_a3xx.c

Signed-off-by: Anshuman Dani <adani@codeaurora.org>
(cherry picked from commit c8efccfc10d531ed9e3805e3f54309f1ca715686)

Signed-off-by: Sudhir Sharma <sudsha@codeaurora.org>
drivers: gpu: msm: change merge from ICS_C to JB_C.

https://review-android.quicinc.com/#/c/144219

Also allows drawctxt_draw_workaround to take a context

Signed-off-by: Matthew Trusten <mtrusten@codeaurora.org>
(cherry picked from commit 0ccd273ad09a403bc7b1be72156e4f6d39cc70f9)

Change-Id: I1fc33313cbb26a0137ca3edaaf14edd6841ac08e
Signed-off-by: Sudhir Sharma <sudsha@codeaurora.org>
msm: kgsl: Idle the GPU after recovery in recovery routine

Before returning from recovery routine, wait for the GPU to
become completely idle to ensure that recovery passed

Signed-off-by: Shubhraprakash Das <sadas@codeaurora.org>
(cherry picked from commit 1088bdb727a0360e0acf38b4420a259877deb125)

Change-Id: Ida471d10188f55f7a5f70bb2427fdac27c8d767d
Signed-off-by: Tarun Karra <tkarra@codeaurora.org>
(cherry picked from commit 776c108db6c62578a0ee618f96088649baf76bd8)

Signed-off-by: Sudhir Sharma <sudsha@codeaurora.org>
msm: kgsl: Start the sleep timer after recovery has completed

Start the sleep timer if recovery from hang is successful.
This prevents GPU from entering low power states during
recovery.

Signed-off-by: Shubhraprakash Das <sadas@codeaurora.org>
(cherry picked from commit df609308d9cdd5ebc4605d09be4fdda420e1a434)

Change-Id: I7034ad26b12175fc704656eced5ea6cf7b57413c
Signed-off-by: Tarun Karra <tkarra@codeaurora.org>
(cherry picked from commit f7593196a0fb7a1563bed6b96297c3c3d1b77e68)

Signed-off-by: Sudhir Sharma <sudsha@codeaurora.org>
msm: kgsl: Always add a start identifier to a command sequence

Add command sequence start identifier for commands submitted from
kgsl as well since this will be used during recovery. We will just
try recovery and not try to differentiate between commands from
kgsl or from user space.

Signed-off-by: Shubhraprakash Das <sadas@codeaurora.org>
(cherry picked from commit c3ad5803d537142ae15841afefba0831e9ed6134)

Change-Id: Ibe363682e4ea405a81fae77d5b5bfeccbad97edc
Signed-off-by: Tarun Karra <tkarra@codeaurora.org>
(cherry picked from commit b13c30dd3ad88b5a5dd128f4824876f82e695b5b)

Signed-off-by: Sudhir Sharma <sudsha@codeaurora.org>
msm: kgsl: Introduce recovery data structure for hang recovery

Create a new data structure that holds all the information required
for recovering from hangs. This data structure is initialized as soon
as hang is detected because if the GPU is under DOS attack then by
the time we start recovery the recovery data may have changed.

Signed-off-by: Shubhraprakash Das <sadas@codeaurora.org>
(cherry picked from commit ba6c70b85941267c552b154be1729aef4e944da6)

Change-Id: Idcd7de7a02b4d57735c2186db9307a22bfd1a6b6
Signed-off-by: Tarun Karra <tkarra@codeaurora.org>
(cherry picked from commit a5667011435a34bedb0916abc80e3337da68f28f)

Signed-off-by: Sudhir Sharma <sudsha@codeaurora.org>
msm: kgsl: Use recovery data structure to extract valid commands

Use the data set in the recovery structure when extracting
valid commands from the ringbuffer instead of reading
it from the device/memory.

Signed-off-by: Shubhraprakash Das <sadas@codeaurora.org>
(cherry picked from commit adb1602262d7a8cf988ef1ced622ea6cc1aecc99)

Change-Id: I5b4f263c34c09bc724e875866806971931060d52
Signed-off-by: Tarun Karra <tkarra@codeaurora.org>
(cherry picked from commit ae6b074d30e3d1d3cad362f80ed5eb4c84f7e001)

Signed-off-by: Sudhir Sharma <sudsha@codeaurora.org>
msm: kgsl: Create a separate function to extract valid commands

Create a separate function that extracts valid commands from good
contexts and invalid commands from bad contexts during recovery

Signed-off-by: Shubhraprakash Das <sadas@codeaurora.org>
(cherry picked from commit 1d577fe6fc5cdf768c5f3fe59b56adcd0e86db19)

Change-Id: I03e787657d37a13cf2c915677ba05b87b5a5731c
Signed-off-by: Tarun Karra <tkarra@codeaurora.org>
(cherry picked from commit 9790eadcb74289fe3114c8cf2ba6d4d8a4920bfd)

Signed-off-by: Sudhir Sharma <sudsha@codeaurora.org>
msm: kgsl: Separate function to detect last command in recovery

Make the code more readable by writing a separate function to
detect the end of last command that is assumed to be successfully
executed by the GPU

Signed-off-by: Shubhraprakash Das <sadas@codeaurora.org>
(cherry picked from commit bb7b32a2cd7c42783f608fa750b7cb23821a587e)

Signed-off-by: Tarun Karra <tkarra@codeaurora.org>
(cherry picked from commit 59c917c7d6e36e5daf9820dc12a3f7c5bd508e2a)

Conflicts:

	drivers/gpu/msm/adreno_ringbuffer.h

Change-Id: Ia9065c8cf83c3404d82a2cf0865e1f0398be69da
Signed-off-by: Sudhir Sharma <sudsha@codeaurora.org>

Conflicts:

	drivers/gpu/msm/adreno_ringbuffer.h
msm: kgsl: Turn on preamble to enable replay of commands

Turn on the preamble of the IB sequence in which hang is assumed
to have happened so that the bad sequence can be replayed.

Signed-off-by: Shubhraprakash Das <sadas@codeaurora.org>
(cherry picked from commit 2a85f1fb19278fa530cb31dca0198583b77fc8be)

Change-Id: Ie3f70f2c8f273f73f5ed525f911bb2914b6bebce
Signed-off-by: Tarun Karra <tkarra@codeaurora.org>
(cherry picked from commit 53b95e6361dc7925374d375ea83cb6f51af59282)

Signed-off-by: Sudhir Sharma <sudsha@codeaurora.org>
msm: kgsl: Do not restore per context timestamp states

We do not have to restore the per context timestamp states during
recovery as this data is irrelevant after recovery.

Signed-off-by: Shubhraprakash Das <sadas@codeaurora.org>
(cherry picked from commit 1caba1c8066b77d3955300c95a9be2489418b094)

Change-Id: I2ebf10cb9fca40fdff9f02978bdcda18dd771871
Signed-off-by: Tarun Karra <tkarra@codeaurora.org>
(cherry picked from commit 5ef7b562609b5b1ac3159f9dfca6d9922ac66186)

Signed-off-by: Sudhir Sharma <sudsha@codeaurora.org>
msm: kgsl: Write a separate function to set the reset status

Write a separate function that sets the reset status of each
context after recovery.

Signed-off-by: Shubhraprakash Das <sadas@codeaurora.org>
(cherry picked from commit 29ed38ee8491fd873b304f3d2edad2a4a0395017)

Change-Id: Ic4a70823cb35637794a281142181d93b689d3b6e
Signed-off-by: Tarun Karra <tkarra@codeaurora.org>
(cherry picked from commit 51d5c7fb21d7d603a085c331bf2d29b5b6754ec3)

Signed-off-by: Sudhir Sharma <sudsha@codeaurora.org>
msm: kgsl: Mark the hung context in recovery before extraction

Mark the context that has caused a hang in recovery before
calling the extraction function. This updates the context
state correctly early in recovery.

Signed-off-by: Shubhraprakash Das <sadas@codeaurora.org>
(cherry picked from commit b9f1e831b78da1192c189aab507272298ae9eb9b)

Change-Id: I8a3078ca76d7f447d5ca104ba98b34d46a4409aa
Signed-off-by: Tarun Karra <tkarra@codeaurora.org>
(cherry picked from commit 790998de09e2fc4a091da9444bad52d222776390)

Signed-off-by: Sudhir Sharma <sudsha@codeaurora.org>
msm: kgsl: Expire timestamps after recovery

Expire the global timestamp and timestamps of contexts that have
cause at the end of recovery. This will prevent any users waiting
on timestamps of hung contexts to return without waiting.

Signed-off-by: Shubhraprakash Das <sadas@codeaurora.org>
(cherry picked from commit 5f085f45b19b1fd209eef1cb02b2bb68ad40f505)

Change-Id: I916bf41bad7b2e33c5fe0a885c3ea4e132974541
Signed-off-by: Tarun Karra <tkarra@codeaurora.org>
(cherry picked from commit c1f92ca72dc3819c93a6bbee21d0af8354919465)

Signed-off-by: Sudhir Sharma <sudsha@codeaurora.org>
msm: kgsl: In recovery search for command stream after global eop

In recovery search for the timestamp of the command stream after
global eop instead of searching for the command stream of the
global eop and then moving the pointer forward till the start of
the command stream following global eop. This is because if the
ringbuffer is full then the global eop timestamp may have been
overwritten, so its more reliable to search for the timestamp
that occurs after global eop.

Signed-off-by: Shubhraprakash Das <sadas@codeaurora.org>
(cherry picked from commit 6f6ecb3ef3eb66ef8e07ae8473682f8b36091376)

Change-Id: I21e28acade3d6f431a00b8825845dbf4b8cb7008
Signed-off-by: Tarun Karra <tkarra@codeaurora.org>
(cherry picked from commit f96febefc320b9c6007f2c167428a7c85e8d56b0)

Signed-off-by: Sudhir Sharma <sudsha@codeaurora.org>
msm: kgsl: After timeout check if timestamp has retired

When a thread times out while waiting for a timestamp, check once
whether the timestamp which it was waiting for has expired or not
before treating it as an error

Signed-off-by: Shubhraprakash Das <sadas@codeaurora.org>
(cherry picked from commit 54396e5406b5fecdd1a0e347dab953967d0e9318)

Change-Id: Iaf8fef4881ca0510ac44b437d82b1382ce602097
Signed-off-by: Tarun Karra <tkarra@codeaurora.org>
(cherry picked from commit cb579bd248409c6892f4eecd0bf93383b66b375d)

Signed-off-by: Sudhir Sharma <sudsha@codeaurora.org>
msm: kgsl: Increase the virtual GPU range for the IOMMU

Instead of a fixed 256MB virtual range for both the GPUMMU and IOMMU, make
the virtual range a property of the MMU engine and set the IOMMU range to
2GB.  Technically we could go all the way up to 4G, but even 2G is far out
of the realm of possiblity in the current generation, and we wanted to
reserve some of the space for future enhancements.

Change-Id: Ic0dedbad2987beb162b6a1878dd65ffae8a78522
Signed-off-by: Jordan Crouse <jcrouse@codeaurora.org>
(cherry picked from commit 6d76c4d0dff8edb0849ec85537d7013640d7d28a)
msm: kgsl: Do bounds checking on user supplied GPU addresses

Add bounds checking to kgsl_sharedmem_find_region to ensure that GPU
addresses supplied by the user via KGSL_IOCTL_SHAREMEM_FREE and
KGSL_IOCTL_CFF_SYNCMEM are within a valid range for the MMU scheme of
choice.

CRs-fixed: 380296
Signed-off-by: Jordan Crouse <jcrouse@codeaurora.org>
(cherry picked from commit e22e21d93260cee3aff0dff7247635d9624c8aa5)

Signed-off-by: Jordan Crouse <jcrouse@codeaurora.org>
(cherry picked from commit 64faf92b2159ae2505ba35f88df15d9c287d2fe6)

Conflicts:

	drivers/gpu/msm/kgsl_mmu.h

Change-Id: I043038d96a4f6f48e4f3391a9ebbccff1afce334
Signed-off-by: Ramakrishna Prasad N <crpn@codeaurora.org>
msm: kgsl: Check for valid pointer before dereferencing

Check to make sure that there is a valid context pointer before
dereferencing it when adding commands to the ringbuffer.

Change-Id: Icf9824de8f62a28cdeeef4378246d9fb334d39fa
Signed-off-by: Carter Cooper <ccooper@codeaurora.org>
msm: kgsl: Always submit an interrupt request to the GPU

A context does not have to be present to send an internal dummy
command to the GPU.  Remove BUG() in this case and instead send
a NULL context which will still cause the interrupt to get submitted
to the GPU.

CRs-fixed: 392688
Change-Id: I66b6eebe899ab09e6615d08faddea7ea73de5c6d
Signed-off-by: Carter Cooper <ccooper@codeaurora.org>