drm-mm.rst 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  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 Manager
  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. .. kernel-doc:: drivers/gpu/drm/ttm/ttm_module.c
  27. :doc: TTM
  28. .. kernel-doc:: include/drm/ttm/ttm_caching.h
  29. :internal:
  30. TTM device object reference
  31. ---------------------------
  32. .. kernel-doc:: include/drm/ttm/ttm_device.h
  33. :internal:
  34. .. kernel-doc:: drivers/gpu/drm/ttm/ttm_device.c
  35. :export:
  36. TTM resource placement reference
  37. --------------------------------
  38. .. kernel-doc:: include/drm/ttm/ttm_placement.h
  39. :internal:
  40. TTM resource object reference
  41. -----------------------------
  42. .. kernel-doc:: include/drm/ttm/ttm_resource.h
  43. :internal:
  44. .. kernel-doc:: drivers/gpu/drm/ttm/ttm_resource.c
  45. :export:
  46. TTM TT object reference
  47. -----------------------
  48. .. kernel-doc:: include/drm/ttm/ttm_tt.h
  49. :internal:
  50. .. kernel-doc:: drivers/gpu/drm/ttm/ttm_tt.c
  51. :export:
  52. TTM page pool reference
  53. -----------------------
  54. .. kernel-doc:: include/drm/ttm/ttm_pool.h
  55. :internal:
  56. .. kernel-doc:: drivers/gpu/drm/ttm/ttm_pool.c
  57. :export:
  58. The Graphics Execution Manager (GEM)
  59. ====================================
  60. The GEM design approach has resulted in a memory manager that doesn't
  61. provide full coverage of all (or even all common) use cases in its
  62. userspace or kernel API. GEM exposes a set of standard memory-related
  63. operations to userspace and a set of helper functions to drivers, and
  64. let drivers implement hardware-specific operations with their own
  65. private API.
  66. The GEM userspace API is described in the `GEM - the Graphics Execution
  67. Manager <http://lwn.net/Articles/283798/>`__ article on LWN. While
  68. slightly outdated, the document provides a good overview of the GEM API
  69. principles. Buffer allocation and read and write operations, described
  70. as part of the common GEM API, are currently implemented using
  71. driver-specific ioctls.
  72. GEM is data-agnostic. It manages abstract buffer objects without knowing
  73. what individual buffers contain. APIs that require knowledge of buffer
  74. contents or purpose, such as buffer allocation or synchronization
  75. primitives, are thus outside of the scope of GEM and must be implemented
  76. using driver-specific ioctls.
  77. On a fundamental level, GEM involves several operations:
  78. - Memory allocation and freeing
  79. - Command execution
  80. - Aperture management at command execution time
  81. Buffer object allocation is relatively straightforward and largely
  82. provided by Linux's shmem layer, which provides memory to back each
  83. object.
  84. Device-specific operations, such as command execution, pinning, buffer
  85. read & write, mapping, and domain ownership transfers are left to
  86. driver-specific ioctls.
  87. GEM Initialization
  88. ------------------
  89. Drivers that use GEM must set the DRIVER_GEM bit in the struct
  90. :c:type:`struct drm_driver <drm_driver>` driver_features
  91. field. The DRM core will then automatically initialize the GEM core
  92. before calling the load operation. Behind the scene, this will create a
  93. DRM Memory Manager object which provides an address space pool for
  94. object allocation.
  95. In a KMS configuration, drivers need to allocate and initialize a
  96. command ring buffer following core GEM initialization if required by the
  97. hardware. UMA devices usually have what is called a "stolen" memory
  98. region, which provides space for the initial framebuffer and large,
  99. contiguous memory regions required by the device. This space is
  100. typically not managed by GEM, and must be initialized separately into
  101. its own DRM MM object.
  102. GEM Objects Creation
  103. --------------------
  104. GEM splits creation of GEM objects and allocation of the memory that
  105. backs them in two distinct operations.
  106. GEM objects are represented by an instance of struct :c:type:`struct
  107. drm_gem_object <drm_gem_object>`. Drivers usually need to
  108. extend GEM objects with private information and thus create a
  109. driver-specific GEM object structure type that embeds an instance of
  110. struct :c:type:`struct drm_gem_object <drm_gem_object>`.
  111. To create a GEM object, a driver allocates memory for an instance of its
  112. specific GEM object type and initializes the embedded struct
  113. :c:type:`struct drm_gem_object <drm_gem_object>` with a call
  114. to drm_gem_object_init(). The function takes a pointer
  115. to the DRM device, a pointer to the GEM object and the buffer object
  116. size in bytes.
  117. GEM uses shmem to allocate anonymous pageable memory.
  118. drm_gem_object_init() will create an shmfs file of the
  119. requested size and store it into the struct :c:type:`struct
  120. drm_gem_object <drm_gem_object>` filp field. The memory is
  121. used as either main storage for the object when the graphics hardware
  122. uses system memory directly or as a backing store otherwise.
  123. Drivers are responsible for the actual physical pages allocation by
  124. calling shmem_read_mapping_page_gfp() for each page.
  125. Note that they can decide to allocate pages when initializing the GEM
  126. object, or to delay allocation until the memory is needed (for instance
  127. when a page fault occurs as a result of a userspace memory access or
  128. when the driver needs to start a DMA transfer involving the memory).
  129. Anonymous pageable memory allocation is not always desired, for instance
  130. when the hardware requires physically contiguous system memory as is
  131. often the case in embedded devices. Drivers can create GEM objects with
  132. no shmfs backing (called private GEM objects) by initializing them with a call
  133. to drm_gem_private_object_init() instead of drm_gem_object_init(). Storage for
  134. private GEM objects must be managed by drivers.
  135. GEM Objects Lifetime
  136. --------------------
  137. All GEM objects are reference-counted by the GEM core. References can be
  138. acquired and release by calling drm_gem_object_get() and drm_gem_object_put()
  139. respectively.
  140. When the last reference to a GEM object is released the GEM core calls
  141. the :c:type:`struct drm_gem_object_funcs <gem_object_funcs>` free
  142. operation. That operation is mandatory for GEM-enabled drivers and must
  143. free the GEM object and all associated resources.
  144. void (\*free) (struct drm_gem_object \*obj); Drivers are
  145. responsible for freeing all GEM object resources. This includes the
  146. resources created by the GEM core, which need to be released with
  147. drm_gem_object_release().
  148. GEM Objects Naming
  149. ------------------
  150. Communication between userspace and the kernel refers to GEM objects
  151. using local handles, global names or, more recently, file descriptors.
  152. All of those are 32-bit integer values; the usual Linux kernel limits
  153. apply to the file descriptors.
  154. GEM handles are local to a DRM file. Applications get a handle to a GEM
  155. object through a driver-specific ioctl, and can use that handle to refer
  156. to the GEM object in other standard or driver-specific ioctls. Closing a
  157. DRM file handle frees all its GEM handles and dereferences the
  158. associated GEM objects.
  159. To create a handle for a GEM object drivers call drm_gem_handle_create(). The
  160. function takes a pointer to the DRM file and the GEM object and returns a
  161. locally unique handle. When the handle is no longer needed drivers delete it
  162. with a call to drm_gem_handle_delete(). Finally the GEM object associated with a
  163. handle can be retrieved by a call to drm_gem_object_lookup().
  164. Handles don't take ownership of GEM objects, they only take a reference
  165. to the object that will be dropped when the handle is destroyed. To
  166. avoid leaking GEM objects, drivers must make sure they drop the
  167. reference(s) they own (such as the initial reference taken at object
  168. creation time) as appropriate, without any special consideration for the
  169. handle. For example, in the particular case of combined GEM object and
  170. handle creation in the implementation of the dumb_create operation,
  171. drivers must drop the initial reference to the GEM object before
  172. returning the handle.
  173. GEM names are similar in purpose to handles but are not local to DRM
  174. files. They can be passed between processes to reference a GEM object
  175. globally. Names can't be used directly to refer to objects in the DRM
  176. API, applications must convert handles to names and names to handles
  177. using the DRM_IOCTL_GEM_FLINK and DRM_IOCTL_GEM_OPEN ioctls
  178. respectively. The conversion is handled by the DRM core without any
  179. driver-specific support.
  180. GEM also supports buffer sharing with dma-buf file descriptors through
  181. PRIME. GEM-based drivers must use the provided helpers functions to
  182. implement the exporting and importing correctly. See ?. Since sharing
  183. file descriptors is inherently more secure than the easily guessable and
  184. global GEM names it is the preferred buffer sharing mechanism. Sharing
  185. buffers through GEM names is only supported for legacy userspace.
  186. Furthermore PRIME also allows cross-device buffer sharing since it is
  187. based on dma-bufs.
  188. GEM Objects Mapping
  189. -------------------
  190. Because mapping operations are fairly heavyweight GEM favours
  191. read/write-like access to buffers, implemented through driver-specific
  192. ioctls, over mapping buffers to userspace. However, when random access
  193. to the buffer is needed (to perform software rendering for instance),
  194. direct access to the object can be more efficient.
  195. The mmap system call can't be used directly to map GEM objects, as they
  196. don't have their own file handle. Two alternative methods currently
  197. co-exist to map GEM objects to userspace. The first method uses a
  198. driver-specific ioctl to perform the mapping operation, calling
  199. do_mmap() under the hood. This is often considered
  200. dubious, seems to be discouraged for new GEM-enabled drivers, and will
  201. thus not be described here.
  202. The second method uses the mmap system call on the DRM file handle. void
  203. \*mmap(void \*addr, size_t length, int prot, int flags, int fd, off_t
  204. offset); DRM identifies the GEM object to be mapped by a fake offset
  205. passed through the mmap offset argument. Prior to being mapped, a GEM
  206. object must thus be associated with a fake offset. To do so, drivers
  207. must call drm_gem_create_mmap_offset() on the object.
  208. Once allocated, the fake offset value must be passed to the application
  209. in a driver-specific way and can then be used as the mmap offset
  210. argument.
  211. The GEM core provides a helper method drm_gem_mmap() to
  212. handle object mapping. The method can be set directly as the mmap file
  213. operation handler. It will look up the GEM object based on the offset
  214. value and set the VMA operations to the :c:type:`struct drm_driver
  215. <drm_driver>` gem_vm_ops field. Note that drm_gem_mmap() doesn't map memory to
  216. userspace, but relies on the driver-provided fault handler to map pages
  217. individually.
  218. To use drm_gem_mmap(), drivers must fill the struct :c:type:`struct drm_driver
  219. <drm_driver>` gem_vm_ops field with a pointer to VM operations.
  220. The VM operations is a :c:type:`struct vm_operations_struct <vm_operations_struct>`
  221. made up of several fields, the more interesting ones being:
  222. .. code-block:: c
  223. struct vm_operations_struct {
  224. void (*open)(struct vm_area_struct * area);
  225. void (*close)(struct vm_area_struct * area);
  226. vm_fault_t (*fault)(struct vm_fault *vmf);
  227. };
  228. The open and close operations must update the GEM object reference
  229. count. Drivers can use the drm_gem_vm_open() and drm_gem_vm_close() helper
  230. functions directly as open and close handlers.
  231. The fault operation handler is responsible for mapping individual pages
  232. to userspace when a page fault occurs. Depending on the memory
  233. allocation scheme, drivers can allocate pages at fault time, or can
  234. decide to allocate memory for the GEM object at the time the object is
  235. created.
  236. Drivers that want to map the GEM object upfront instead of handling page
  237. faults can implement their own mmap file operation handler.
  238. For platforms without MMU the GEM core provides a helper method
  239. drm_gem_dma_get_unmapped_area(). The mmap() routines will call this to get a
  240. proposed address for the mapping.
  241. To use drm_gem_dma_get_unmapped_area(), drivers must fill the struct
  242. :c:type:`struct file_operations <file_operations>` get_unmapped_area field with
  243. a pointer on drm_gem_dma_get_unmapped_area().
  244. More detailed information about get_unmapped_area can be found in
  245. Documentation/admin-guide/mm/nommu-mmap.rst
  246. Memory Coherency
  247. ----------------
  248. When mapped to the device or used in a command buffer, backing pages for
  249. an object are flushed to memory and marked write combined so as to be
  250. coherent with the GPU. Likewise, if the CPU accesses an object after the
  251. GPU has finished rendering to the object, then the object must be made
  252. coherent with the CPU's view of memory, usually involving GPU cache
  253. flushing of various kinds. This core CPU<->GPU coherency management is
  254. provided by a device-specific ioctl, which evaluates an object's current
  255. domain and performs any necessary flushing or synchronization to put the
  256. object into the desired coherency domain (note that the object may be
  257. busy, i.e. an active render target; in that case, setting the domain
  258. blocks the client and waits for rendering to complete before performing
  259. any necessary flushing operations).
  260. Command Execution
  261. -----------------
  262. Perhaps the most important GEM function for GPU devices is providing a
  263. command execution interface to clients. Client programs construct
  264. command buffers containing references to previously allocated memory
  265. objects, and then submit them to GEM. At that point, GEM takes care to
  266. bind all the objects into the GTT, execute the buffer, and provide
  267. necessary synchronization between clients accessing the same buffers.
  268. This often involves evicting some objects from the GTT and re-binding
  269. others (a fairly expensive operation), and providing relocation support
  270. which hides fixed GTT offsets from clients. Clients must take care not
  271. to submit command buffers that reference more objects than can fit in
  272. the GTT; otherwise, GEM will reject them and no rendering will occur.
  273. Similarly, if several objects in the buffer require fence registers to
  274. be allocated for correct rendering (e.g. 2D blits on pre-965 chips),
  275. care must be taken not to require more fence registers than are
  276. available to the client. Such resource management should be abstracted
  277. from the client in libdrm.
  278. GEM Function Reference
  279. ----------------------
  280. .. kernel-doc:: include/drm/drm_gem.h
  281. :internal:
  282. .. kernel-doc:: drivers/gpu/drm/drm_gem.c
  283. :export:
  284. GEM DMA Helper Functions Reference
  285. ----------------------------------
  286. .. kernel-doc:: drivers/gpu/drm/drm_gem_dma_helper.c
  287. :doc: dma helpers
  288. .. kernel-doc:: include/drm/drm_gem_dma_helper.h
  289. :internal:
  290. .. kernel-doc:: drivers/gpu/drm/drm_gem_dma_helper.c
  291. :export:
  292. GEM SHMEM Helper Function Reference
  293. -----------------------------------
  294. .. kernel-doc:: drivers/gpu/drm/drm_gem_shmem_helper.c
  295. :doc: overview
  296. .. kernel-doc:: include/drm/drm_gem_shmem_helper.h
  297. :internal:
  298. .. kernel-doc:: drivers/gpu/drm/drm_gem_shmem_helper.c
  299. :export:
  300. GEM VRAM Helper Functions Reference
  301. -----------------------------------
  302. .. kernel-doc:: drivers/gpu/drm/drm_gem_vram_helper.c
  303. :doc: overview
  304. .. kernel-doc:: include/drm/drm_gem_vram_helper.h
  305. :internal:
  306. .. kernel-doc:: drivers/gpu/drm/drm_gem_vram_helper.c
  307. :export:
  308. GEM TTM Helper Functions Reference
  309. -----------------------------------
  310. .. kernel-doc:: drivers/gpu/drm/drm_gem_ttm_helper.c
  311. :doc: overview
  312. .. kernel-doc:: drivers/gpu/drm/drm_gem_ttm_helper.c
  313. :export:
  314. VMA Offset Manager
  315. ==================
  316. .. kernel-doc:: drivers/gpu/drm/drm_vma_manager.c
  317. :doc: vma offset manager
  318. .. kernel-doc:: include/drm/drm_vma_manager.h
  319. :internal:
  320. .. kernel-doc:: drivers/gpu/drm/drm_vma_manager.c
  321. :export:
  322. .. _prime_buffer_sharing:
  323. PRIME Buffer Sharing
  324. ====================
  325. PRIME is the cross device buffer sharing framework in drm, originally
  326. created for the OPTIMUS range of multi-gpu platforms. To userspace PRIME
  327. buffers are dma-buf based file descriptors.
  328. Overview and Lifetime Rules
  329. ---------------------------
  330. .. kernel-doc:: drivers/gpu/drm/drm_prime.c
  331. :doc: overview and lifetime rules
  332. PRIME Helper Functions
  333. ----------------------
  334. .. kernel-doc:: drivers/gpu/drm/drm_prime.c
  335. :doc: PRIME Helpers
  336. PRIME Function References
  337. -------------------------
  338. .. kernel-doc:: include/drm/drm_prime.h
  339. :internal:
  340. .. kernel-doc:: drivers/gpu/drm/drm_prime.c
  341. :export:
  342. DRM MM Range Allocator
  343. ======================
  344. Overview
  345. --------
  346. .. kernel-doc:: drivers/gpu/drm/drm_mm.c
  347. :doc: Overview
  348. LRU Scan/Eviction Support
  349. -------------------------
  350. .. kernel-doc:: drivers/gpu/drm/drm_mm.c
  351. :doc: lru scan roster
  352. DRM MM Range Allocator Function References
  353. ------------------------------------------
  354. .. kernel-doc:: include/drm/drm_mm.h
  355. :internal:
  356. .. kernel-doc:: drivers/gpu/drm/drm_mm.c
  357. :export:
  358. DRM Buddy Allocator
  359. ===================
  360. DRM Buddy Function References
  361. -----------------------------
  362. .. kernel-doc:: drivers/gpu/drm/drm_buddy.c
  363. :export:
  364. DRM Cache Handling and Fast WC memcpy()
  365. =======================================
  366. .. kernel-doc:: drivers/gpu/drm/drm_cache.c
  367. :export:
  368. DRM Sync Objects
  369. ===========================
  370. .. kernel-doc:: drivers/gpu/drm/drm_syncobj.c
  371. :doc: Overview
  372. .. kernel-doc:: include/drm/drm_syncobj.h
  373. :internal:
  374. .. kernel-doc:: drivers/gpu/drm/drm_syncobj.c
  375. :export:
  376. GPU Scheduler
  377. =============
  378. Overview
  379. --------
  380. .. kernel-doc:: drivers/gpu/drm/scheduler/sched_main.c
  381. :doc: Overview
  382. Scheduler Function References
  383. -----------------------------
  384. .. kernel-doc:: include/drm/gpu_scheduler.h
  385. :internal:
  386. .. kernel-doc:: drivers/gpu/drm/scheduler/sched_main.c
  387. :export:
  388. .. kernel-doc:: drivers/gpu/drm/scheduler/sched_entity.c
  389. :export: