drm-mm.rst 19 KB

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