msm_gem.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*
  2. * Copyright (C) 2013 Red Hat
  3. * Author: Rob Clark <[email protected]>
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2 as published by
  7. * the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along with
  15. * this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #ifndef __MSM_GEM_H__
  18. #define __MSM_GEM_H__
  19. #include <linux/kref.h>
  20. #include <linux/reservation.h>
  21. #include "msm_drv.h"
  22. /* Additional internal-use only BO flags: */
  23. #define MSM_BO_STOLEN 0x10000000 /* try to use stolen/splash memory */
  24. #define MSM_BO_KEEPATTRS 0x20000000 /* keep h/w bus attributes */
  25. #define MSM_BO_SKIPSYNC 0x40000000 /* skip dmabuf cpu sync */
  26. #define MSM_BO_EXTBUF 0x80000000 /* indicate BO is an import buffer */
  27. struct msm_gem_object;
  28. struct msm_gem_aspace_ops {
  29. int (*map)(struct msm_gem_address_space *space, struct msm_gem_vma *vma,
  30. struct sg_table *sgt, int npages, unsigned int flags);
  31. void (*unmap)(struct msm_gem_address_space *space,
  32. struct msm_gem_vma *vma, struct sg_table *sgt,
  33. unsigned int flags);
  34. void (*destroy)(struct msm_gem_address_space *space);
  35. void (*add_to_active)(struct msm_gem_address_space *space,
  36. struct msm_gem_object *obj);
  37. void (*remove_from_active)(struct msm_gem_address_space *space,
  38. struct msm_gem_object *obj);
  39. int (*register_cb)(struct msm_gem_address_space *space,
  40. void (*cb)(void *cb, bool data),
  41. void *cb_data);
  42. int (*unregister_cb)(struct msm_gem_address_space *space,
  43. void (*cb)(void *cb, bool data),
  44. void *cb_data);
  45. };
  46. struct aspace_client {
  47. void (*cb)(void *cb, bool data);
  48. void *cb_data;
  49. struct list_head list;
  50. };
  51. struct msm_gem_address_space {
  52. const char *name;
  53. /* NOTE: mm managed at the page level, size is in # of pages
  54. * and position mm_node->start is in # of pages:
  55. */
  56. struct drm_mm mm;
  57. spinlock_t lock; /* Protects drm_mm node allocation/removal */
  58. struct msm_mmu *mmu;
  59. struct kref kref;
  60. bool domain_attached;
  61. const struct msm_gem_aspace_ops *ops;
  62. struct drm_device *dev;
  63. /* list of mapped objects */
  64. struct list_head active_list;
  65. /* list of clients */
  66. struct list_head clients;
  67. struct mutex list_lock; /* Protects active_list & clients */
  68. };
  69. struct msm_gem_vma {
  70. struct drm_mm_node node;
  71. uint64_t iova;
  72. struct msm_gem_address_space *aspace;
  73. struct list_head list; /* node in msm_gem_object::vmas */
  74. };
  75. struct msm_gem_object {
  76. struct drm_gem_object base;
  77. uint32_t flags;
  78. /**
  79. * Advice: are the backing pages purgeable?
  80. */
  81. uint8_t madv;
  82. /**
  83. * count of active vmap'ing
  84. */
  85. uint8_t vmap_count;
  86. /* And object is either:
  87. * inactive - on priv->inactive_list
  88. * active - on one one of the gpu's active_list.. well, at
  89. * least for now we don't have (I don't think) hw sync between
  90. * 2d and 3d one devices which have both, meaning we need to
  91. * block on submit if a bo is already on other ring
  92. *
  93. */
  94. struct list_head mm_list;
  95. struct msm_gpu *gpu; /* non-null if active */
  96. /* Transiently in the process of submit ioctl, objects associated
  97. * with the submit are on submit->bo_list.. this only lasts for
  98. * the duration of the ioctl, so one bo can never be on multiple
  99. * submit lists.
  100. */
  101. struct list_head submit_entry;
  102. struct page **pages;
  103. struct sg_table *sgt;
  104. void *vaddr;
  105. struct list_head vmas; /* list of msm_gem_vma */
  106. /* normally (resv == &_resv) except for imported bo's */
  107. struct reservation_object *resv;
  108. struct reservation_object _resv;
  109. /* For physically contiguous buffers. Used when we don't have
  110. * an IOMMU. Also used for stolen/splashscreen buffer.
  111. */
  112. struct drm_mm_node *vram_node;
  113. struct mutex lock; /* Protects resources associated with bo */
  114. struct list_head iova_list;
  115. struct msm_gem_address_space *aspace;
  116. bool in_active_list;
  117. };
  118. #define to_msm_bo(x) container_of(x, struct msm_gem_object, base)
  119. static inline bool is_active(struct msm_gem_object *msm_obj)
  120. {
  121. return msm_obj->gpu != NULL;
  122. }
  123. static inline bool is_purgeable(struct msm_gem_object *msm_obj)
  124. {
  125. WARN_ON(!mutex_is_locked(&msm_obj->base.dev->struct_mutex));
  126. return (msm_obj->madv == MSM_MADV_DONTNEED) && msm_obj->sgt &&
  127. !msm_obj->base.dma_buf && !msm_obj->base.import_attach;
  128. }
  129. static inline bool is_vunmapable(struct msm_gem_object *msm_obj)
  130. {
  131. return (msm_obj->vmap_count == 0) && msm_obj->vaddr;
  132. }
  133. /* The shrinker can be triggered while we hold objA->lock, and need
  134. * to grab objB->lock to purge it. Lockdep just sees these as a single
  135. * class of lock, so we use subclasses to teach it the difference.
  136. *
  137. * OBJ_LOCK_NORMAL is implicit (ie. normal mutex_lock() call), and
  138. * OBJ_LOCK_SHRINKER is used by shrinker.
  139. *
  140. * It is *essential* that we never go down paths that could trigger the
  141. * shrinker for a purgable object. This is ensured by checking that
  142. * msm_obj->madv == MSM_MADV_WILLNEED.
  143. */
  144. enum msm_gem_lock {
  145. OBJ_LOCK_NORMAL,
  146. OBJ_LOCK_SHRINKER,
  147. };
  148. void msm_gem_purge(struct drm_gem_object *obj, enum msm_gem_lock subclass);
  149. void msm_gem_vunmap(struct drm_gem_object *obj, enum msm_gem_lock subclass);
  150. /* Created per submit-ioctl, to track bo's and cmdstream bufs, etc,
  151. * associated with the cmdstream submission for synchronization (and
  152. * make it easier to unwind when things go wrong, etc). This only
  153. * lasts for the duration of the submit-ioctl.
  154. */
  155. struct msm_gem_submit {
  156. struct drm_device *dev;
  157. struct msm_gpu *gpu;
  158. struct list_head node; /* node in ring submit list */
  159. struct list_head bo_list;
  160. struct ww_acquire_ctx ticket;
  161. uint32_t seqno; /* Sequence number of the submit on the ring */
  162. struct dma_fence *fence;
  163. struct msm_gpu_submitqueue *queue;
  164. struct pid *pid; /* submitting process */
  165. bool valid; /* true if no cmdstream patching needed */
  166. bool in_rb; /* "sudo" mode, copy cmds into RB */
  167. struct msm_ringbuffer *ring;
  168. unsigned int nr_cmds;
  169. unsigned int nr_bos;
  170. struct {
  171. uint32_t type;
  172. uint32_t size; /* in dwords */
  173. uint64_t iova;
  174. uint32_t idx; /* cmdstream buffer idx in bos[] */
  175. } *cmd; /* array of size nr_cmds */
  176. struct {
  177. uint32_t flags;
  178. struct msm_gem_object *obj;
  179. uint64_t iova;
  180. } bos[0];
  181. };
  182. #endif /* __MSM_GEM_H__ */