drm-mm.rst 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. =====================
  2. DRM Memory Management
  3. =====================
  4. Modern Linux systems require large amount of graphics memory to store
  5. frame buffers, textures, vertices and other graphics-related data. Given
  6. the very dynamic nature of many of that data, managing graphics memory
  7. efficiently is thus crucial for the graphics stack and plays a central
  8. role in the DRM infrastructure.
  9. The DRM core includes two memory managers, namely Translation Table Maps
  10. (TTM) and Graphics Execution Manager (GEM). TTM was the first DRM memory
  11. manager to be developed and tried to be a one-size-fits-them all
  12. solution. It provides a single userspace API to accommodate the need of
  13. all hardware, supporting both Unified Memory Architecture (UMA) devices
  14. and devices with dedicated video RAM (i.e. most discrete video cards).
  15. This resulted in a large, complex piece of code that turned out to be
  16. hard to use for driver development.
  17. GEM started as an Intel-sponsored project in reaction to TTM's
  18. complexity. Its design philosophy is completely different: instead of
  19. providing a solution to every graphics memory-related problems, GEM
  20. identified common code between drivers and created a support library to
  21. share it. GEM has simpler initialization and execution requirements than
  22. TTM, but has no video RAM management capabilities and is thus limited to
  23. UMA devices.
  24. The Translation Table Manager (TTM)
  25. ===================================
  26. TTM design background and information belongs here.
  27. TTM initialization
  28. ------------------
  29. **Warning**
  30. This section is outdated.
  31. Drivers wishing to support TTM must fill out a drm_bo_driver
  32. structure. The structure contains several fields with function pointers
  33. for initializing the TTM, allocating and freeing memory, waiting for
  34. command completion and fence synchronization, and memory migration. See
  35. the radeon_ttm.c file for an example of usage.
  36. The ttm_global_reference structure is made up of several fields:
  37. ::
  38. struct ttm_global_reference {
  39. enum ttm_global_types global_type;
  40. size_t size;
  41. void *object;
  42. int (*init) (struct ttm_global_reference *);
  43. void (*release) (struct ttm_global_reference *);
  44. };
  45. There should be one global reference structure for your memory manager
  46. as a whole, and there will be others for each object created by the
  47. memory manager at runtime. Your global TTM should have a type of
  48. TTM_GLOBAL_TTM_MEM. The size field for the global object should be
  49. sizeof(struct ttm_mem_global), and the init and release hooks should
  50. point at your driver-specific init and release routines, which probably
  51. eventually call ttm_mem_global_init and ttm_mem_global_release,
  52. respectively.
  53. Once your global TTM accounting structure is set up and initialized by
  54. calling ttm_global_item_ref() on it, you need to create a buffer
  55. object TTM to provide a pool for buffer object allocation by clients and
  56. the kernel itself. The type of this object should be
  57. TTM_GLOBAL_TTM_BO, and its size should be sizeof(struct
  58. ttm_bo_global). Again, driver-specific init and release functions may
  59. be provided, likely eventually calling ttm_bo_global_init() and
  60. ttm_bo_global_release(), respectively. Also, like the previous
  61. object, ttm_global_item_ref() is used to create an initial reference
  62. count for the TTM, which will call your initialization function.
  63. The Graphics Execution Manager (GEM)
  64. ====================================
  65. The GEM design approach has resulted in a memory manager that doesn't
  66. provide full coverage of all (or even all common) use cases in its
  67. userspace or kernel API. GEM exposes a set of standard memory-related
  68. operations to userspace and a set of helper functions to drivers, and
  69. let drivers implement hardware-specific operations with their own
  70. private API.
  71. The GEM userspace API is described in the `GEM - the Graphics Execution
  72. Manager <http://lwn.net/Articles/283798/>`__ article on LWN. While
  73. slightly outdated, the document provides a good overview of the GEM API
  74. principles. Buffer allocation and read and write operations, described
  75. as part of the common GEM API, are currently implemented using
  76. driver-specific ioctls.
  77. GEM is data-agnostic. It manages abstract buffer objects without knowing
  78. what individual buffers contain. APIs that require knowledge of buffer
  79. contents or purpose, such as buffer allocation or synchronization
  80. primitives, are thus outside of the scope of GEM and must be implemented
  81. using driver-specific ioctls.
  82. On a fundamental level, GEM involves several operations:
  83. - Memory allocation and freeing
  84. - Command execution
  85. - Aperture management at command execution time
  86. Buffer object allocation is relatively straightforward and largely
  87. provided by Linux's shmem layer, which provides memory to back each
  88. object.
  89. Device-specific operations, such as command execution, pinning, buffer
  90. read & write, mapping, and domain ownership transfers are left to
  91. driver-specific ioctls.
  92. GEM Initialization
  93. ------------------
  94. Drivers that use GEM must set the DRIVER_GEM bit in the struct
  95. :c:type:`struct drm_driver <drm_driver>` driver_features
  96. field. The DRM core will then automatically initialize the GEM core
  97. before calling the load operation. Behind the scene, this will create a
  98. DRM Memory Manager object which provides an address space pool for
  99. object allocation.
  100. In a KMS configuration, drivers need to allocate and initialize a
  101. command ring buffer following core GEM initialization if required by the
  102. hardware. UMA devices usually have what is called a "stolen" memory
  103. region, which provides space for the initial framebuffer and large,
  104. contiguous memory regions required by the device. This space is
  105. typically not managed by GEM, and must be initialized separately into
  106. its own DRM MM object.
  107. GEM Objects Creation
  108. --------------------
  109. GEM splits creation of GEM objects and allocation of the memory that
  110. backs them in two distinct operations.
  111. GEM objects are represented by an instance of struct :c:type:`struct
  112. drm_gem_object <drm_gem_object>`. Drivers usually need to
  113. extend GEM objects with private information and thus create a
  114. driver-specific GEM object structure type that embeds an instance of
  115. struct :c:type:`struct drm_gem_object <drm_gem_object>`.
  116. To create a GEM object, a driver allocates memory for an instance of its
  117. specific GEM object type and initializes the embedded struct
  118. :c:type:`struct drm_gem_object <drm_gem_object>` with a call
  119. to :c:func:`drm_gem_object_init()`. The function takes a pointer
  120. to the DRM device, a pointer to the GEM object and the buffer object
  121. size in bytes.
  122. GEM uses shmem to allocate anonymous pageable memory.
  123. :c:func:`drm_gem_object_init()` will create an shmfs file of the
  124. requested size and store it into the struct :c:type:`struct
  125. drm_gem_object <drm_gem_object>` filp field. The memory is
  126. used as either main storage for the object when the graphics hardware
  127. uses system memory directly or as a backing store otherwise.
  128. Drivers are responsible for the actual physical pages allocation by
  129. calling :c:func:`shmem_read_mapping_page_gfp()` for each page.
  130. Note that they can decide to allocate pages when initializing the GEM
  131. object, or to delay allocation until the memory is needed (for instance
  132. when a page fault occurs as a result of a userspace memory access or
  133. when the driver needs to start a DMA transfer involving the memory).
  134. Anonymous pageable memory allocation is not always desired, for instance
  135. when the hardware requires physically contiguous system memory as is
  136. often the case in embedded devices. Drivers can create GEM objects with
  137. no shmfs backing (called private GEM objects) by initializing them with
  138. a call to :c:func:`drm_gem_private_object_init()` instead of
  139. :c:func:`drm_gem_object_init()`. Storage for private GEM objects
  140. must be managed by drivers.
  141. GEM Objects Lifetime
  142. --------------------
  143. All GEM objects are reference-counted by the GEM core. References can be
  144. acquired and release by :c:func:`calling
  145. drm_gem_object_reference()` and
  146. :c:func:`drm_gem_object_unreference()` respectively. The caller
  147. must hold the :c:type:`struct drm_device <drm_device>`
  148. struct_mutex lock when calling
  149. :c:func:`drm_gem_object_reference()`. As a convenience, GEM
  150. provides :c:func:`drm_gem_object_unreference_unlocked()`
  151. functions that can be called without holding the lock.
  152. When the last reference to a GEM object is released the GEM core calls
  153. the :c:type:`struct drm_driver <drm_driver>` gem_free_object
  154. operation. That operation is mandatory for GEM-enabled drivers and must
  155. free the GEM object and all associated resources.
  156. void (\*gem_free_object) (struct drm_gem_object \*obj); Drivers are
  157. responsible for freeing all GEM object resources. This includes the
  158. resources created by the GEM core, which need to be released with
  159. :c:func:`drm_gem_object_release()`.
  160. GEM Objects Naming
  161. ------------------
  162. Communication between userspace and the kernel refers to GEM objects
  163. using local handles, global names or, more recently, file descriptors.
  164. All of those are 32-bit integer values; the usual Linux kernel limits
  165. apply to the file descriptors.
  166. GEM handles are local to a DRM file. Applications get a handle to a GEM
  167. object through a driver-specific ioctl, and can use that handle to refer
  168. to the GEM object in other standard or driver-specific ioctls. Closing a
  169. DRM file handle frees all its GEM handles and dereferences the
  170. associated GEM objects.
  171. To create a handle for a GEM object drivers call
  172. :c:func:`drm_gem_handle_create()`. The function takes a pointer
  173. to the DRM file and the GEM object and returns a locally unique handle.
  174. When the handle is no longer needed drivers delete it with a call to
  175. :c:func:`drm_gem_handle_delete()`. Finally the GEM object
  176. associated with a handle can be retrieved by a call to
  177. :c:func:`drm_gem_object_lookup()`.
  178. Handles don't take ownership of GEM objects, they only take a reference
  179. to the object that will be dropped when the handle is destroyed. To
  180. avoid leaking GEM objects, drivers must make sure they drop the
  181. reference(s) they own (such as the initial reference taken at object
  182. creation time) as appropriate, without any special consideration for the
  183. handle. For example, in the particular case of combined GEM object and
  184. handle creation in the implementation of the dumb_create operation,
  185. drivers must drop the initial reference to the GEM object before
  186. returning the handle.
  187. GEM names are similar in purpose to handles but are not local to DRM
  188. files. They can be passed between processes to reference a GEM object
  189. globally. Names can't be used directly to refer to objects in the DRM
  190. API, applications must convert handles to names and names to handles
  191. using the DRM_IOCTL_GEM_FLINK and DRM_IOCTL_GEM_OPEN ioctls
  192. respectively. The conversion is handled by the DRM core without any
  193. driver-specific support.
  194. GEM also supports buffer sharing with dma-buf file descriptors through
  195. PRIME. GEM-based drivers must use the provided helpers functions to
  196. implement the exporting and importing correctly. See ?. Since sharing
  197. file descriptors is inherently more secure than the easily guessable and
  198. global GEM names it is the preferred buffer sharing mechanism. Sharing
  199. buffers through GEM names is only supported for legacy userspace.
  200. Furthermore PRIME also allows cross-device buffer sharing since it is
  201. based on dma-bufs.
  202. GEM Objects Mapping
  203. -------------------
  204. Because mapping operations are fairly heavyweight GEM favours
  205. read/write-like access to buffers, implemented through driver-specific
  206. ioctls, over mapping buffers to userspace. However, when random access
  207. to the buffer is needed (to perform software rendering for instance),
  208. direct access to the object can be more efficient.
  209. The mmap system call can't be used directly to map GEM objects, as they
  210. don't have their own file handle. Two alternative methods currently
  211. co-exist to map GEM objects to userspace. The first method uses a
  212. driver-specific ioctl to perform the mapping operation, calling
  213. :c:func:`do_mmap()` under the hood. This is often considered
  214. dubious, seems to be discouraged for new GEM-enabled drivers, and will
  215. thus not be described here.
  216. The second method uses the mmap system call on the DRM file handle. void
  217. \*mmap(void \*addr, size_t length, int prot, int flags, int fd, off_t
  218. offset); DRM identifies the GEM object to be mapped by a fake offset
  219. passed through the mmap offset argument. Prior to being mapped, a GEM
  220. object must thus be associated with a fake offset. To do so, drivers
  221. must call :c:func:`drm_gem_create_mmap_offset()` on the object.
  222. Once allocated, the fake offset value must be passed to the application
  223. in a driver-specific way and can then be used as the mmap offset
  224. argument.
  225. The GEM core provides a helper method :c:func:`drm_gem_mmap()` to
  226. handle object mapping. The method can be set directly as the mmap file
  227. operation handler. It will look up the GEM object based on the offset
  228. value and set the VMA operations to the :c:type:`struct drm_driver
  229. <drm_driver>` gem_vm_ops field. Note that
  230. :c:func:`drm_gem_mmap()` doesn't map memory to userspace, but
  231. relies on the driver-provided fault handler to map pages individually.
  232. To use :c:func:`drm_gem_mmap()`, drivers must fill the struct
  233. :c:type:`struct drm_driver <drm_driver>` gem_vm_ops field
  234. with a pointer to VM operations.
  235. struct vm_operations_struct \*gem_vm_ops struct
  236. vm_operations_struct { void (\*open)(struct vm_area_struct \* area);
  237. void (\*close)(struct vm_area_struct \* area); int (\*fault)(struct
  238. vm_area_struct \*vma, struct vm_fault \*vmf); };
  239. The open and close operations must update the GEM object reference
  240. count. Drivers can use the :c:func:`drm_gem_vm_open()` and
  241. :c:func:`drm_gem_vm_close()` helper functions directly as open
  242. and close handlers.
  243. The fault operation handler is responsible for mapping individual pages
  244. to userspace when a page fault occurs. Depending on the memory
  245. allocation scheme, drivers can allocate pages at fault time, or can
  246. decide to allocate memory for the GEM object at the time the object is
  247. created.
  248. Drivers that want to map the GEM object upfront instead of handling page
  249. faults can implement their own mmap file operation handler.
  250. Memory Coherency
  251. ----------------
  252. When mapped to the device or used in a command buffer, backing pages for
  253. an object are flushed to memory and marked write combined so as to be
  254. coherent with the GPU. Likewise, if the CPU accesses an object after the
  255. GPU has finished rendering to the object, then the object must be made
  256. coherent with the CPU's view of memory, usually involving GPU cache
  257. flushing of various kinds. This core CPU<->GPU coherency management is
  258. provided by a device-specific ioctl, which evaluates an object's current
  259. domain and performs any necessary flushing or synchronization to put the
  260. object into the desired coherency domain (note that the object may be
  261. busy, i.e. an active render target; in that case, setting the domain
  262. blocks the client and waits for rendering to complete before performing
  263. any necessary flushing operations).
  264. Command Execution
  265. -----------------
  266. Perhaps the most important GEM function for GPU devices is providing a
  267. command execution interface to clients. Client programs construct
  268. command buffers containing references to previously allocated memory
  269. objects, and then submit them to GEM. At that point, GEM takes care to
  270. bind all the objects into the GTT, execute the buffer, and provide
  271. necessary synchronization between clients accessing the same buffers.
  272. This often involves evicting some objects from the GTT and re-binding
  273. others (a fairly expensive operation), and providing relocation support
  274. which hides fixed GTT offsets from clients. Clients must take care not
  275. to submit command buffers that reference more objects than can fit in
  276. the GTT; otherwise, GEM will reject them and no rendering will occur.
  277. Similarly, if several objects in the buffer require fence registers to
  278. be allocated for correct rendering (e.g. 2D blits on pre-965 chips),
  279. care must be taken not to require more fence registers than are
  280. available to the client. Such resource management should be abstracted
  281. from the client in libdrm.
  282. GEM Function Reference
  283. ----------------------
  284. .. kernel-doc:: drivers/gpu/drm/drm_gem.c
  285. :export:
  286. .. kernel-doc:: include/drm/drm_gem.h
  287. :internal:
  288. GEM CMA Helper Functions Reference
  289. ----------------------------------
  290. .. kernel-doc:: drivers/gpu/drm/drm_gem_cma_helper.c
  291. :doc: cma helpers
  292. .. kernel-doc:: drivers/gpu/drm/drm_gem_cma_helper.c
  293. :export:
  294. .. kernel-doc:: include/drm/drm_gem_cma_helper.h
  295. :internal:
  296. VMA Offset Manager
  297. ==================
  298. .. kernel-doc:: drivers/gpu/drm/drm_vma_manager.c
  299. :doc: vma offset manager
  300. .. kernel-doc:: drivers/gpu/drm/drm_vma_manager.c
  301. :export:
  302. .. kernel-doc:: include/drm/drm_vma_manager.h
  303. :internal:
  304. PRIME Buffer Sharing
  305. ====================
  306. PRIME is the cross device buffer sharing framework in drm, originally
  307. created for the OPTIMUS range of multi-gpu platforms. To userspace PRIME
  308. buffers are dma-buf based file descriptors.
  309. Overview and Driver Interface
  310. -----------------------------
  311. Similar to GEM global names, PRIME file descriptors are also used to
  312. share buffer objects across processes. They offer additional security:
  313. as file descriptors must be explicitly sent over UNIX domain sockets to
  314. be shared between applications, they can't be guessed like the globally
  315. unique GEM names.
  316. Drivers that support the PRIME API must set the DRIVER_PRIME bit in the
  317. struct :c:type:`struct drm_driver <drm_driver>`
  318. driver_features field, and implement the prime_handle_to_fd and
  319. prime_fd_to_handle operations.
  320. int (\*prime_handle_to_fd)(struct drm_device \*dev, struct drm_file
  321. \*file_priv, uint32_t handle, uint32_t flags, int \*prime_fd); int
  322. (\*prime_fd_to_handle)(struct drm_device \*dev, struct drm_file
  323. \*file_priv, int prime_fd, uint32_t \*handle); Those two operations
  324. convert a handle to a PRIME file descriptor and vice versa. Drivers must
  325. use the kernel dma-buf buffer sharing framework to manage the PRIME file
  326. descriptors. Similar to the mode setting API PRIME is agnostic to the
  327. underlying buffer object manager, as long as handles are 32bit unsigned
  328. integers.
  329. While non-GEM drivers must implement the operations themselves, GEM
  330. drivers must use the :c:func:`drm_gem_prime_handle_to_fd()` and
  331. :c:func:`drm_gem_prime_fd_to_handle()` helper functions. Those
  332. helpers rely on the driver gem_prime_export and gem_prime_import
  333. operations to create a dma-buf instance from a GEM object (dma-buf
  334. exporter role) and to create a GEM object from a dma-buf instance
  335. (dma-buf importer role).
  336. struct dma_buf \* (\*gem_prime_export)(struct drm_device \*dev,
  337. struct drm_gem_object \*obj, int flags); struct drm_gem_object \*
  338. (\*gem_prime_import)(struct drm_device \*dev, struct dma_buf
  339. \*dma_buf); These two operations are mandatory for GEM drivers that
  340. support PRIME.
  341. PRIME Helper Functions
  342. ----------------------
  343. .. kernel-doc:: drivers/gpu/drm/drm_prime.c
  344. :doc: PRIME Helpers
  345. PRIME Function References
  346. -------------------------
  347. .. kernel-doc:: drivers/gpu/drm/drm_prime.c
  348. :export:
  349. DRM MM Range Allocator
  350. ======================
  351. Overview
  352. --------
  353. .. kernel-doc:: drivers/gpu/drm/drm_mm.c
  354. :doc: Overview
  355. LRU Scan/Eviction Support
  356. -------------------------
  357. .. kernel-doc:: drivers/gpu/drm/drm_mm.c
  358. :doc: lru scan roaster
  359. DRM MM Range Allocator Function References
  360. ------------------------------------------
  361. .. kernel-doc:: drivers/gpu/drm/drm_mm.c
  362. :export:
  363. .. kernel-doc:: include/drm/drm_mm.h
  364. :internal: