drm-mm.rst 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  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. Drivers
  123. can call drm_gem_huge_mnt_create() to create, mount and use a huge
  124. shmem mountpoint instead of the default one ('shm_mnt'). For builds
  125. with CONFIG_TRANSPARENT_HUGEPAGE enabled, further calls to
  126. drm_gem_object_init() will let shmem allocate huge pages when
  127. possible.
  128. Drivers are responsible for the actual physical pages allocation by
  129. calling 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 a call
  138. to drm_gem_private_object_init() instead of drm_gem_object_init(). Storage for
  139. private GEM objects must be managed by drivers.
  140. GEM Objects Lifetime
  141. --------------------
  142. All GEM objects are reference-counted by the GEM core. References can be
  143. acquired and release by calling drm_gem_object_get() and drm_gem_object_put()
  144. respectively.
  145. When the last reference to a GEM object is released the GEM core calls
  146. the :c:type:`struct drm_gem_object_funcs <gem_object_funcs>` free
  147. operation. That operation is mandatory for GEM-enabled drivers and must
  148. free the GEM object and all associated resources.
  149. void (\*free) (struct drm_gem_object \*obj); Drivers are
  150. responsible for freeing all GEM object resources. This includes the
  151. resources created by the GEM core, which need to be released with
  152. drm_gem_object_release().
  153. GEM Objects Naming
  154. ------------------
  155. Communication between userspace and the kernel refers to GEM objects
  156. using local handles, global names or, more recently, file descriptors.
  157. All of those are 32-bit integer values; the usual Linux kernel limits
  158. apply to the file descriptors.
  159. GEM handles are local to a DRM file. Applications get a handle to a GEM
  160. object through a driver-specific ioctl, and can use that handle to refer
  161. to the GEM object in other standard or driver-specific ioctls. Closing a
  162. DRM file handle frees all its GEM handles and dereferences the
  163. associated GEM objects.
  164. To create a handle for a GEM object drivers call drm_gem_handle_create(). The
  165. function takes a pointer to the DRM file and the GEM object and returns a
  166. locally unique handle. When the handle is no longer needed drivers delete it
  167. with a call to drm_gem_handle_delete(). Finally the GEM object associated with a
  168. handle can be retrieved by a call to drm_gem_object_lookup().
  169. Handles don't take ownership of GEM objects, they only take a reference
  170. to the object that will be dropped when the handle is destroyed. To
  171. avoid leaking GEM objects, drivers must make sure they drop the
  172. reference(s) they own (such as the initial reference taken at object
  173. creation time) as appropriate, without any special consideration for the
  174. handle. For example, in the particular case of combined GEM object and
  175. handle creation in the implementation of the dumb_create operation,
  176. drivers must drop the initial reference to the GEM object before
  177. returning the handle.
  178. GEM names are similar in purpose to handles but are not local to DRM
  179. files. They can be passed between processes to reference a GEM object
  180. globally. Names can't be used directly to refer to objects in the DRM
  181. API, applications must convert handles to names and names to handles
  182. using the DRM_IOCTL_GEM_FLINK and DRM_IOCTL_GEM_OPEN ioctls
  183. respectively. The conversion is handled by the DRM core without any
  184. driver-specific support.
  185. GEM also supports buffer sharing with dma-buf file descriptors through
  186. PRIME. GEM-based drivers must use the provided helpers functions to
  187. implement the exporting and importing correctly. See ?. Since sharing
  188. file descriptors is inherently more secure than the easily guessable and
  189. global GEM names it is the preferred buffer sharing mechanism. Sharing
  190. buffers through GEM names is only supported for legacy userspace.
  191. Furthermore PRIME also allows cross-device buffer sharing since it is
  192. based on dma-bufs.
  193. GEM Objects Mapping
  194. -------------------
  195. Because mapping operations are fairly heavyweight GEM favours
  196. read/write-like access to buffers, implemented through driver-specific
  197. ioctls, over mapping buffers to userspace. However, when random access
  198. to the buffer is needed (to perform software rendering for instance),
  199. direct access to the object can be more efficient.
  200. The mmap system call can't be used directly to map GEM objects, as they
  201. don't have their own file handle. Two alternative methods currently
  202. co-exist to map GEM objects to userspace. The first method uses a
  203. driver-specific ioctl to perform the mapping operation, calling
  204. do_mmap() under the hood. This is often considered
  205. dubious, seems to be discouraged for new GEM-enabled drivers, and will
  206. thus not be described here.
  207. The second method uses the mmap system call on the DRM file handle. void
  208. \*mmap(void \*addr, size_t length, int prot, int flags, int fd, off_t
  209. offset); DRM identifies the GEM object to be mapped by a fake offset
  210. passed through the mmap offset argument. Prior to being mapped, a GEM
  211. object must thus be associated with a fake offset. To do so, drivers
  212. must call drm_gem_create_mmap_offset() on the object.
  213. Once allocated, the fake offset value must be passed to the application
  214. in a driver-specific way and can then be used as the mmap offset
  215. argument.
  216. The GEM core provides a helper method drm_gem_mmap() to
  217. handle object mapping. The method can be set directly as the mmap file
  218. operation handler. It will look up the GEM object based on the offset
  219. value and set the VMA operations to the :c:type:`struct drm_driver
  220. <drm_driver>` gem_vm_ops field. Note that drm_gem_mmap() doesn't map memory to
  221. userspace, but relies on the driver-provided fault handler to map pages
  222. individually.
  223. To use drm_gem_mmap(), drivers must fill the struct :c:type:`struct drm_driver
  224. <drm_driver>` gem_vm_ops field with a pointer to VM operations.
  225. The VM operations is a :c:type:`struct vm_operations_struct <vm_operations_struct>`
  226. made up of several fields, the more interesting ones being:
  227. .. code-block:: c
  228. struct vm_operations_struct {
  229. void (*open)(struct vm_area_struct * area);
  230. void (*close)(struct vm_area_struct * area);
  231. vm_fault_t (*fault)(struct vm_fault *vmf);
  232. };
  233. The open and close operations must update the GEM object reference
  234. count. Drivers can use the drm_gem_vm_open() and drm_gem_vm_close() helper
  235. functions directly as open and close handlers.
  236. The fault operation handler is responsible for mapping pages to
  237. userspace when a page fault occurs. Depending on the memory allocation
  238. scheme, drivers can allocate pages at fault time, or can decide to
  239. allocate memory for the GEM object at the time the object is created.
  240. Drivers that want to map the GEM object upfront instead of handling page
  241. faults can implement their own mmap file operation handler.
  242. In order to reduce page table overhead, if the internal shmem mountpoint
  243. "shm_mnt" is configured to use transparent huge pages (for builds with
  244. CONFIG_TRANSPARENT_HUGEPAGE enabled) and if the shmem backing store
  245. managed to allocate a huge page for a faulty address, the fault handler
  246. will first attempt to insert that huge page into the VMA before falling
  247. back to individual page insertion. mmap() user address alignment for GEM
  248. objects is handled by providing a custom get_unmapped_area file
  249. operation which forwards to the shmem backing store. For most drivers,
  250. which don't create a huge mountpoint by default or through a module
  251. parameter, transparent huge pages can be enabled by either setting the
  252. "transparent_hugepage_shmem" kernel parameter or the
  253. "/sys/kernel/mm/transparent_hugepage/shmem_enabled" sysfs knob.
  254. For platforms without MMU the GEM core provides a helper method
  255. drm_gem_dma_get_unmapped_area(). The mmap() routines will call this to get a
  256. proposed address for the mapping.
  257. To use drm_gem_dma_get_unmapped_area(), drivers must fill the struct
  258. :c:type:`struct file_operations <file_operations>` get_unmapped_area field with
  259. a pointer on drm_gem_dma_get_unmapped_area().
  260. More detailed information about get_unmapped_area can be found in
  261. Documentation/admin-guide/mm/nommu-mmap.rst
  262. Memory Coherency
  263. ----------------
  264. When mapped to the device or used in a command buffer, backing pages for
  265. an object are flushed to memory and marked write combined so as to be
  266. coherent with the GPU. Likewise, if the CPU accesses an object after the
  267. GPU has finished rendering to the object, then the object must be made
  268. coherent with the CPU's view of memory, usually involving GPU cache
  269. flushing of various kinds. This core CPU<->GPU coherency management is
  270. provided by a device-specific ioctl, which evaluates an object's current
  271. domain and performs any necessary flushing or synchronization to put the
  272. object into the desired coherency domain (note that the object may be
  273. busy, i.e. an active render target; in that case, setting the domain
  274. blocks the client and waits for rendering to complete before performing
  275. any necessary flushing operations).
  276. Command Execution
  277. -----------------
  278. Perhaps the most important GEM function for GPU devices is providing a
  279. command execution interface to clients. Client programs construct
  280. command buffers containing references to previously allocated memory
  281. objects, and then submit them to GEM. At that point, GEM takes care to
  282. bind all the objects into the GTT, execute the buffer, and provide
  283. necessary synchronization between clients accessing the same buffers.
  284. This often involves evicting some objects from the GTT and re-binding
  285. others (a fairly expensive operation), and providing relocation support
  286. which hides fixed GTT offsets from clients. Clients must take care not
  287. to submit command buffers that reference more objects than can fit in
  288. the GTT; otherwise, GEM will reject them and no rendering will occur.
  289. Similarly, if several objects in the buffer require fence registers to
  290. be allocated for correct rendering (e.g. 2D blits on pre-965 chips),
  291. care must be taken not to require more fence registers than are
  292. available to the client. Such resource management should be abstracted
  293. from the client in libdrm.
  294. GEM Function Reference
  295. ----------------------
  296. .. kernel-doc:: include/drm/drm_gem.h
  297. :internal:
  298. .. kernel-doc:: drivers/gpu/drm/drm_gem.c
  299. :export:
  300. GEM DMA Helper Functions Reference
  301. ----------------------------------
  302. .. kernel-doc:: drivers/gpu/drm/drm_gem_dma_helper.c
  303. :doc: dma helpers
  304. .. kernel-doc:: include/drm/drm_gem_dma_helper.h
  305. :internal:
  306. .. kernel-doc:: drivers/gpu/drm/drm_gem_dma_helper.c
  307. :export:
  308. GEM SHMEM Helper Function Reference
  309. -----------------------------------
  310. .. kernel-doc:: drivers/gpu/drm/drm_gem_shmem_helper.c
  311. :doc: overview
  312. .. kernel-doc:: include/drm/drm_gem_shmem_helper.h
  313. :internal:
  314. .. kernel-doc:: drivers/gpu/drm/drm_gem_shmem_helper.c
  315. :export:
  316. GEM VRAM Helper Functions Reference
  317. -----------------------------------
  318. .. kernel-doc:: drivers/gpu/drm/drm_gem_vram_helper.c
  319. :doc: overview
  320. .. kernel-doc:: include/drm/drm_gem_vram_helper.h
  321. :internal:
  322. .. kernel-doc:: drivers/gpu/drm/drm_gem_vram_helper.c
  323. :export:
  324. GEM TTM Helper Functions Reference
  325. -----------------------------------
  326. .. kernel-doc:: drivers/gpu/drm/drm_gem_ttm_helper.c
  327. :doc: overview
  328. .. kernel-doc:: drivers/gpu/drm/drm_gem_ttm_helper.c
  329. :export:
  330. VMA Offset Manager
  331. ==================
  332. .. kernel-doc:: drivers/gpu/drm/drm_vma_manager.c
  333. :doc: vma offset manager
  334. .. kernel-doc:: include/drm/drm_vma_manager.h
  335. :internal:
  336. .. kernel-doc:: drivers/gpu/drm/drm_vma_manager.c
  337. :export:
  338. .. _prime_buffer_sharing:
  339. PRIME Buffer Sharing
  340. ====================
  341. PRIME is the cross device buffer sharing framework in drm, originally
  342. created for the OPTIMUS range of multi-gpu platforms. To userspace PRIME
  343. buffers are dma-buf based file descriptors.
  344. Overview and Lifetime Rules
  345. ---------------------------
  346. .. kernel-doc:: drivers/gpu/drm/drm_prime.c
  347. :doc: overview and lifetime rules
  348. PRIME Helper Functions
  349. ----------------------
  350. .. kernel-doc:: drivers/gpu/drm/drm_prime.c
  351. :doc: PRIME Helpers
  352. PRIME Function References
  353. -------------------------
  354. .. kernel-doc:: include/drm/drm_prime.h
  355. :internal:
  356. .. kernel-doc:: drivers/gpu/drm/drm_prime.c
  357. :export:
  358. DRM MM Range Allocator
  359. ======================
  360. Overview
  361. --------
  362. .. kernel-doc:: drivers/gpu/drm/drm_mm.c
  363. :doc: Overview
  364. LRU Scan/Eviction Support
  365. -------------------------
  366. .. kernel-doc:: drivers/gpu/drm/drm_mm.c
  367. :doc: lru scan roster
  368. DRM MM Range Allocator Function References
  369. ------------------------------------------
  370. .. kernel-doc:: include/drm/drm_mm.h
  371. :internal:
  372. .. kernel-doc:: drivers/gpu/drm/drm_mm.c
  373. :export:
  374. .. _drm_gpuvm:
  375. DRM GPUVM
  376. =========
  377. Overview
  378. --------
  379. .. kernel-doc:: drivers/gpu/drm/drm_gpuvm.c
  380. :doc: Overview
  381. Split and Merge
  382. ---------------
  383. .. kernel-doc:: drivers/gpu/drm/drm_gpuvm.c
  384. :doc: Split and Merge
  385. .. _drm_gpuvm_locking:
  386. Locking
  387. -------
  388. .. kernel-doc:: drivers/gpu/drm/drm_gpuvm.c
  389. :doc: Locking
  390. Examples
  391. --------
  392. .. kernel-doc:: drivers/gpu/drm/drm_gpuvm.c
  393. :doc: Examples
  394. DRM GPUVM Function References
  395. -----------------------------
  396. .. kernel-doc:: include/drm/drm_gpuvm.h
  397. :internal:
  398. .. kernel-doc:: drivers/gpu/drm/drm_gpuvm.c
  399. :export:
  400. DRM Buddy Allocator
  401. ===================
  402. DRM Buddy Function References
  403. -----------------------------
  404. .. kernel-doc:: drivers/gpu/drm/drm_buddy.c
  405. :export:
  406. DRM Cache Handling and Fast WC memcpy()
  407. =======================================
  408. .. kernel-doc:: drivers/gpu/drm/drm_cache.c
  409. :export:
  410. .. _drm_sync_objects:
  411. DRM Sync Objects
  412. ================
  413. .. kernel-doc:: drivers/gpu/drm/drm_syncobj.c
  414. :doc: Overview
  415. .. kernel-doc:: include/drm/drm_syncobj.h
  416. :internal:
  417. .. kernel-doc:: drivers/gpu/drm/drm_syncobj.c
  418. :export:
  419. DRM Execution context
  420. =====================
  421. .. kernel-doc:: drivers/gpu/drm/drm_exec.c
  422. :doc: Overview
  423. .. kernel-doc:: include/drm/drm_exec.h
  424. :internal:
  425. .. kernel-doc:: drivers/gpu/drm/drm_exec.c
  426. :export:
  427. GPU Scheduler
  428. =============
  429. Overview
  430. --------
  431. .. kernel-doc:: drivers/gpu/drm/scheduler/sched_main.c
  432. :doc: Overview
  433. Flow Control
  434. ------------
  435. .. kernel-doc:: drivers/gpu/drm/scheduler/sched_main.c
  436. :doc: Flow Control
  437. Scheduler Function References
  438. -----------------------------
  439. .. kernel-doc:: include/drm/gpu_scheduler.h
  440. :internal:
  441. .. kernel-doc:: drivers/gpu/drm/scheduler/sched_main.c
  442. :export:
  443. .. kernel-doc:: drivers/gpu/drm/scheduler/sched_entity.c
  444. :export: