cmdq_mutex.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #include <linux/errno.h>
  2. #include <linux/spinlock.h>
  3. #include <linux/string.h>
  4. /* #include <mach/mt_clkmgr.h> */
  5. #include "cmdq_def.h"
  6. #include "cmdq_mutex.h"
  7. #include "cmdq_core.h"
  8. static spinlock_t gMutexLock;
  9. static uint32_t gMutexUsed[DISP_MUTEX_MDP_COUNT];
  10. static pid_t gMutexUserPid[DISP_MUTEX_MDP_COUNT];
  11. static int gMDPMutexCount;
  12. int32_t cmdqMutexInitialize(void)
  13. {
  14. unsigned long flags;
  15. spin_lock_init(&gMutexLock);
  16. spin_lock_irqsave(&gMutexLock, flags);
  17. gMDPMutexCount = 0;
  18. memset(gMutexUsed, 0x0, sizeof(gMutexUsed));
  19. memset(gMutexUserPid, 0x0, sizeof(gMutexUsed));
  20. spin_unlock_irqrestore(&gMutexLock, flags);
  21. return 0;
  22. }
  23. bool cmdqMDPMutexInUse(int index)
  24. {
  25. if (gMutexUsed[index] != 0)
  26. return true;
  27. else
  28. return false;
  29. }
  30. pid_t cmdqMDPMutexOwnerPid(int index)
  31. {
  32. if (gMutexUsed[index] != 0)
  33. return gMutexUserPid[index];
  34. else
  35. return 0;
  36. }
  37. int32_t cmdqMutexAcquire(void)
  38. {
  39. unsigned long flags;
  40. int32_t mutex;
  41. int32_t index;
  42. mutex = -1;
  43. spin_lock_irqsave(&gMutexLock, flags);
  44. for (index = 0; index < DISP_MUTEX_MDP_COUNT; index++) {
  45. if (0 == gMutexUsed[index]) {
  46. /* Record the mutex */
  47. mutex = index;
  48. /* Set to use state */
  49. gMutexUsed[index] = 1;
  50. gMutexUserPid[index] = current->pid;
  51. /* note: although we tracks Mutex usage count, */
  52. /* we do not enable/disable its clock. */
  53. /* this is because some system process like */
  54. /* MMComposerThread may keep mutex for a long time, */
  55. /* even across suspend/resume calls. */
  56. gMDPMutexCount++;
  57. break;
  58. }
  59. }
  60. spin_unlock_irqrestore(&gMutexLock, flags);
  61. if (mutex == -1) {
  62. CMDQ_ERR("cmdqMutexAcquire failed\n");
  63. return mutex;
  64. }
  65. /* note that we have an offset, */
  66. /* the mutex id does NOT start from 0! */
  67. mutex += DISP_MUTEX_MDP_FIRST;
  68. CMDQ_VERBOSE("[MUTEX] acquire mutex %d\n", mutex);
  69. return mutex;
  70. }
  71. int32_t cmdqMutexRelease(int32_t mutex)
  72. {
  73. unsigned long flags;
  74. CMDQ_VERBOSE("[MUTEX] release mutex %d\n", mutex);
  75. mutex -= DISP_MUTEX_MDP_FIRST;
  76. if ((mutex < 0) || (mutex >= DISP_MUTEX_MDP_COUNT)) {
  77. CMDQ_ERR("[MUTEX]wrong mutex offset %d\n", mutex);
  78. return -EFAULT;
  79. }
  80. spin_lock_irqsave(&gMutexLock, flags);
  81. if (1 == gMutexUsed[mutex]) {
  82. /* OK we release a Mutex - clock OFF if no more MUTEX */
  83. gMDPMutexCount--;
  84. /* note: although we tracks Mutex usage count, */
  85. /* we do not enable/disable its clock. */
  86. /* this is because some system process like */
  87. /* MMComposerThread may keep mutex for a long time, */
  88. /* even across suspend/resume calls. */
  89. }
  90. gMutexUsed[mutex] = 0;
  91. spin_unlock_irqrestore(&gMutexLock, flags);
  92. return 0;
  93. }
  94. void cmdqMutexDeInitialize(void)
  95. {
  96. /* Do nothing */
  97. }