msm_gem.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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/dma-resv.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_EXTBUF 0x40000000 /* indicate BO is an import buffer */
  26. struct msm_gem_object;
  27. struct msm_gem_aspace_ops {
  28. int (*map)(struct msm_gem_address_space *space, struct msm_gem_vma *vma,
  29. struct sg_table *sgt, int npages, unsigned int flags);
  30. void (*unmap)(struct msm_gem_address_space *space,
  31. struct msm_gem_vma *vma, struct sg_table *sgt,
  32. unsigned int flags);
  33. void (*destroy)(struct msm_gem_address_space *space);
  34. void (*add_to_active)(struct msm_gem_address_space *space,
  35. struct msm_gem_object *obj);
  36. void (*remove_from_active)(struct msm_gem_address_space *space,
  37. struct msm_gem_object *obj);
  38. int (*register_cb)(struct msm_gem_address_space *space,
  39. void (*cb)(void *cb, bool data),
  40. void *cb_data);
  41. int (*unregister_cb)(struct msm_gem_address_space *space,
  42. void (*cb)(void *cb, bool data),
  43. void *cb_data);
  44. };
  45. struct aspace_client {
  46. void (*cb)(void *cb, bool data);
  47. void *cb_data;
  48. struct list_head list;
  49. };
  50. struct msm_gem_address_space {
  51. const char *name;
  52. /* NOTE: mm managed at the page level, size is in # of pages
  53. * and position mm_node->start is in # of pages:
  54. */
  55. struct drm_mm mm;
  56. spinlock_t lock; /* Protects drm_mm node allocation/removal */
  57. struct msm_mmu *mmu;
  58. struct kref kref;
  59. bool domain_attached;
  60. const struct msm_gem_aspace_ops *ops;
  61. struct drm_device *dev;
  62. /* list of mapped objects */
  63. struct list_head active_list;
  64. /* list of clients */
  65. struct list_head clients;
  66. struct mutex list_lock; /* Protects active_list & clients */
  67. };
  68. struct msm_gem_vma {
  69. struct drm_mm_node node;
  70. uint64_t iova;
  71. struct msm_gem_address_space *aspace;
  72. struct list_head list; /* node in msm_gem_object::vmas */
  73. bool mapped;
  74. int inuse;
  75. };
  76. struct msm_gem_object {
  77. struct drm_gem_object base;
  78. uint32_t flags;
  79. /**
  80. * Advice: are the backing pages purgeable?
  81. */
  82. uint8_t madv;
  83. /**
  84. * count of active vmap'ing
  85. */
  86. uint8_t vmap_count;
  87. /* And object is either:
  88. * inactive - on priv->inactive_list
  89. * active - on one one of the gpu's active_list.. well, at
  90. * least for now we don't have (I don't think) hw sync between
  91. * 2d and 3d one devices which have both, meaning we need to
  92. * block on submit if a bo is already on other ring
  93. *
  94. */
  95. struct list_head mm_list;
  96. struct msm_gpu *gpu; /* non-null if active */
  97. /* Transiently in the process of submit ioctl, objects associated
  98. * with the submit are on submit->bo_list.. this only lasts for
  99. * the duration of the ioctl, so one bo can never be on multiple
  100. * submit lists.
  101. */
  102. struct list_head submit_entry;
  103. struct page **pages;
  104. struct sg_table *sgt;
  105. void *vaddr;
  106. struct list_head vmas; /* list of msm_gem_vma */
  107. struct llist_node freed;
  108. /* normally (resv == &_resv) except for imported bo's */
  109. struct dma_resv *resv;
  110. struct dma_resv _resv;
  111. /* For physically contiguous buffers. Used when we don't have
  112. * an IOMMU. Also used for stolen/splashscreen buffer.
  113. */
  114. struct drm_mm_node *vram_node;
  115. struct mutex lock; /* Protects resources associated with bo */
  116. struct list_head iova_list;
  117. struct msm_gem_address_space *aspace;
  118. bool in_active_list;
  119. char name[32]; /* Identifier to print for the debugfs files */
  120. /* Indicates whether object needs to request for
  121. * new pagetables due to cb switch
  122. */
  123. bool obj_dirty;
  124. /* iova address and aligned offset */
  125. uint64_t iova;
  126. uint32_t offset;
  127. };
  128. #define to_msm_bo(x) container_of(x, struct msm_gem_object, base)
  129. static inline bool is_active(struct msm_gem_object *msm_obj)
  130. {
  131. return msm_obj->gpu != NULL;
  132. }
  133. static inline bool is_vunmapable(struct msm_gem_object *msm_obj)
  134. {
  135. return (msm_obj->vmap_count == 0) && msm_obj->vaddr;
  136. }
  137. /* The shrinker can be triggered while we hold objA->lock, and need
  138. * to grab objB->lock to purge it. Lockdep just sees these as a single
  139. * class of lock, so we use subclasses to teach it the difference.
  140. *
  141. * OBJ_LOCK_NORMAL is implicit (ie. normal mutex_lock() call), and
  142. * OBJ_LOCK_SHRINKER is used by shrinker.
  143. *
  144. * It is *essential* that we never go down paths that could trigger the
  145. * shrinker for a purgable object. This is ensured by checking that
  146. * msm_obj->madv == MSM_MADV_WILLNEED.
  147. */
  148. enum msm_gem_lock {
  149. OBJ_LOCK_NORMAL,
  150. OBJ_LOCK_SHRINKER,
  151. };
  152. void msm_gem_vunmap(struct drm_gem_object *obj, enum msm_gem_lock subclass);
  153. /* Created per submit-ioctl, to track bo's and cmdstream bufs, etc,
  154. * associated with the cmdstream submission for synchronization (and
  155. * make it easier to unwind when things go wrong, etc). This only
  156. * lasts for the duration of the submit-ioctl.
  157. */
  158. struct msm_gem_submit {
  159. struct drm_device *dev;
  160. struct msm_gpu *gpu;
  161. struct list_head node; /* node in ring submit list */
  162. struct list_head bo_list;
  163. struct ww_acquire_ctx ticket;
  164. uint32_t seqno; /* Sequence number of the submit on the ring */
  165. struct dma_fence *fence;
  166. struct msm_gpu_submitqueue *queue;
  167. struct pid *pid; /* submitting process */
  168. bool valid; /* true if no cmdstream patching needed */
  169. bool in_rb; /* "sudo" mode, copy cmds into RB */
  170. struct msm_ringbuffer *ring;
  171. unsigned int nr_cmds;
  172. unsigned int nr_bos;
  173. u32 ident; /* A "identifier" for the submit for logging */
  174. struct {
  175. uint32_t type;
  176. uint32_t size; /* in dwords */
  177. uint64_t iova;
  178. uint32_t idx; /* cmdstream buffer idx in bos[] */
  179. } *cmd; /* array of size nr_cmds */
  180. struct {
  181. uint32_t flags;
  182. struct msm_gem_object *obj;
  183. uint64_t iova;
  184. } bos[0];
  185. };
  186. /**
  187. * msm_gem_put_buffer - put gem buffer
  188. * @gem: pointer to gem buffer object
  189. */
  190. void msm_gem_put_buffer(struct drm_gem_object *gem);
  191. /**
  192. * msm_gem_gem_buffer - get a gem buffer
  193. * @gem: drm gem object
  194. * @drm_device: pointer to drm device
  195. * @fb: frame buffer object
  196. * @align_size: size to align the buffer to
  197. */
  198. int msm_gem_get_buffer(struct drm_gem_object *gem,
  199. struct drm_device *dev, struct drm_framebuffer *fb,
  200. uint32_t align_size);
  201. #endif /* __MSM_GEM_H__ */