Blame man/drm-memory.xml

Packit Service 103f6b
 
Packit Service 103f6b
Packit Service 103f6b
          "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd">
Packit Service 103f6b
Packit Service 103f6b
Packit Service 103f6b
  Written 2012 by David Herrmann <dh.herrmann@googlemail.com>
Packit Service 103f6b
  Dedicated to the Public Domain
Packit Service 103f6b
-->
Packit Service 103f6b
Packit Service 103f6b
<refentry id="drm-memory">
Packit Service 103f6b
  <refentryinfo>
Packit Service 103f6b
    <title>Direct Rendering Manager</title>
Packit Service 103f6b
    <productname>libdrm</productname>
Packit Service 103f6b
    <date>September 2012</date>
Packit Service 103f6b
    <authorgroup>
Packit Service 103f6b
      <author>
Packit Service 103f6b
        <contrib>Developer</contrib>
Packit Service 103f6b
        <firstname>David</firstname>
Packit Service 103f6b
        <surname>Herrmann</surname>
Packit Service 103f6b
        <email>dh.herrmann@googlemail.com</email>
Packit Service 103f6b
      </author>
Packit Service 103f6b
    </authorgroup>
Packit Service 103f6b
  </refentryinfo>
Packit Service 103f6b
Packit Service 103f6b
  <refmeta>
Packit Service 103f6b
    <refentrytitle>drm-memory</refentrytitle>
Packit Service 103f6b
    <manvolnum>7</manvolnum>
Packit Service 103f6b
  </refmeta>
Packit Service 103f6b
Packit Service 103f6b
  <refnamediv>
Packit Service 103f6b
    <refname>drm-memory</refname>
Packit Service 103f6b
    <refname>drm-mm</refname>
Packit Service 103f6b
    <refname>drm-gem</refname>
Packit Service 103f6b
    <refname>drm-ttm</refname>
Packit Service 103f6b
    <refpurpose>DRM Memory Management</refpurpose>
Packit Service 103f6b
  </refnamediv>
Packit Service 103f6b
Packit Service 103f6b
  <refsynopsisdiv>
Packit Service 103f6b
    <funcsynopsis>
Packit Service 103f6b
      <funcsynopsisinfo>#include <xf86drm.h></funcsynopsisinfo>
Packit Service 103f6b
    </funcsynopsis>
Packit Service 103f6b
  </refsynopsisdiv>
Packit Service 103f6b
Packit Service 103f6b
  <refsect1>
Packit Service 103f6b
    <title>Description</title>
Packit Service 103f6b
      <para>Many modern high-end GPUs come with their own memory managers. They
Packit Service 103f6b
            even include several different caches that need to be synchronized
Packit Service 103f6b
            during access. Textures, framebuffers, command buffers and more need
Packit Service 103f6b
            to be stored in memory that can be accessed quickly by the GPU.
Packit Service 103f6b
            Therefore, memory management on GPUs is highly driver- and
Packit Service 103f6b
            hardware-dependent.</para>
Packit Service 103f6b
Packit Service 103f6b
      <para>However, there are several frameworks in the kernel that are used by
Packit Service 103f6b
            more than one driver. These can be used for trivial mode-setting
Packit Service 103f6b
            without requiring driver-dependent code. But for
Packit Service 103f6b
            hardware-accelerated rendering you need to read the manual pages for
Packit Service 103f6b
            the driver you want to work with.</para>
Packit Service 103f6b
Packit Service 103f6b
    <refsect2>
Packit Service 103f6b
      <title>Dumb-Buffers</title>
Packit Service 103f6b
      <para>Almost all in-kernel DRM hardware drivers support an API called
Packit Service 103f6b
            <emphasis>Dumb-Buffers</emphasis>. This API allows to create buffers
Packit Service 103f6b
            of arbitrary size that can be used for scanout. These buffers can be
Packit Service 103f6b
            memory mapped via
Packit Service 103f6b
            <citerefentry><refentrytitle>mmap</refentrytitle><manvolnum>2</manvolnum></citerefentry>
Packit Service 103f6b
            so you can render into them on the CPU. However, GPU access to these
Packit Service 103f6b
            buffers is often not possible. Therefore, they are fine for simple
Packit Service 103f6b
            tasks but not suitable for complex compositions and
Packit Service 103f6b
            renderings.</para>
Packit Service 103f6b
Packit Service 103f6b
      <para>The <constant>DRM_IOCTL_MODE_CREATE_DUMB</constant> ioctl can be
Packit Service 103f6b
            used to create a dumb buffer. The kernel will return a 32bit handle
Packit Service 103f6b
            that can be used to manage the buffer with the DRM API. You can
Packit Service 103f6b
            create framebuffers with
Packit Service 103f6b
            <citerefentry><refentrytitle>drmModeAddFB</refentrytitle><manvolnum>3</manvolnum></citerefentry>
Packit Service 103f6b
            and use it for mode-setting and scanout. To access the buffer, you
Packit Service 103f6b
            first need to retrieve the offset of the buffer. The
Packit Service 103f6b
            <constant>DRM_IOCTL_MODE_MAP_DUMB</constant> ioctl requests the DRM
Packit Service 103f6b
            subsystem to prepare the buffer for memory-mapping and returns a
Packit Service 103f6b
            fake-offset that can be used with
Packit Service 103f6b
            <citerefentry><refentrytitle>mmap</refentrytitle><manvolnum>2</manvolnum></citerefentry>.</para>
Packit Service 103f6b
Packit Service 103f6b
      <para>The <constant>DRM_IOCTL_MODE_CREATE_DUMB</constant> ioctl takes as
Packit Service 103f6b
            argument a structure of type
Packit Service 103f6b
            <structname>struct drm_mode_create_dumb</structname>:
Packit Service 103f6b
Packit Service 103f6b
<programlisting>
Packit Service 103f6b
struct drm_mode_create_dumb {
Packit Service 103f6b
	__u32 height;
Packit Service 103f6b
	__u32 width;
Packit Service 103f6b
	__u32 bpp;
Packit Service 103f6b
	__u32 flags;
Packit Service 103f6b
Packit Service 103f6b
	__u32 handle;
Packit Service 103f6b
	__u32 pitch;
Packit Service 103f6b
	__u64 size;
Packit Service 103f6b
};
Packit Service 103f6b
</programlisting>
Packit Service 103f6b
Packit Service 103f6b
            The fields <structfield>height</structfield>,
Packit Service 103f6b
            <structfield>width</structfield>, <structfield>bpp</structfield> and
Packit Service 103f6b
            <structfield>flags</structfield> have to be provided by the caller.
Packit Service 103f6b
            The other fields are filled by the kernel with the return values.
Packit Service 103f6b
            <structfield>height</structfield> and
Packit Service 103f6b
            <structfield>width</structfield> are the dimensions of the
Packit Service 103f6b
            rectangular buffer that is created. <structfield>bpp</structfield>
Packit Service 103f6b
            is the number of bits-per-pixel and must be a multiple of
Packit Service 103f6b
            <literal>8</literal>. You most commonly want to pass
Packit Service 103f6b
            <literal>32</literal> here. The <structfield>flags</structfield>
Packit Service 103f6b
            field is currently unused and must be zeroed. Different flags to
Packit Service 103f6b
            modify the behavior may be added in the future. After calling the
Packit Service 103f6b
            ioctl, the <structfield>handle</structfield>,
Packit Service 103f6b
            <structfield>pitch</structfield> and <structfield>size</structfield>
Packit Service 103f6b
            fields are filled by the kernel. <structfield>handle</structfield>
Packit Service 103f6b
            is a 32bit gem handle that identifies the buffer. This is used by
Packit Service 103f6b
            several other calls that take a gem-handle or memory-buffer as
Packit Service 103f6b
            argument. The <structfield>pitch</structfield> field is the
Packit Service 103f6b
            pitch (or stride) of the new buffer. Most drivers use 32bit or 64bit
Packit Service 103f6b
            aligned stride-values. The <structfield>size</structfield> field
Packit Service 103f6b
            contains the absolute size in bytes of the buffer. This can normally
Packit Service 103f6b
            also be computed with
Packit Service 103f6b
            <emphasis>(height * pitch + width) * bpp / 4</emphasis>.</para>
Packit Service 103f6b
Packit Service 103f6b
      <para>To prepare the buffer for
Packit Service 103f6b
            <citerefentry><refentrytitle>mmap</refentrytitle><manvolnum>2</manvolnum></citerefentry>
Packit Service 103f6b
            you need to use the <constant>DRM_IOCTL_MODE_MAP_DUMB</constant>
Packit Service 103f6b
            ioctl. It takes as argument a structure of type
Packit Service 103f6b
            <structname>struct drm_mode_map_dumb</structname>:
Packit Service 103f6b
Packit Service 103f6b
<programlisting>
Packit Service 103f6b
struct drm_mode_map_dumb {
Packit Service 103f6b
	__u32 handle;
Packit Service 103f6b
	__u32 pad;
Packit Service 103f6b
Packit Service 103f6b
	__u64 offset;
Packit Service 103f6b
};
Packit Service 103f6b
</programlisting>
Packit Service 103f6b
Packit Service 103f6b
            You need to put the gem-handle that was previously retrieved via
Packit Service 103f6b
            <constant>DRM_IOCTL_MODE_CREATE_DUMB</constant> into the
Packit Service 103f6b
            <structfield>handle</structfield> field. The
Packit Service 103f6b
            <structfield>pad</structfield> field is unused padding and must be
Packit Service 103f6b
            zeroed. After completion, the <structfield>offset</structfield>
Packit Service 103f6b
            field will contain an offset that can be used with
Packit Service 103f6b
            <citerefentry><refentrytitle>mmap</refentrytitle><manvolnum>2</manvolnum></citerefentry>
Packit Service 103f6b
            on the DRM file-descriptor.</para>
Packit Service 103f6b
Packit Service 103f6b
      <para>If you don't need your dumb-buffer, anymore, you have to destroy it
Packit Service 103f6b
            with <constant>DRM_IOCTL_MODE_DESTROY_DUMB</constant>. If you close
Packit Service 103f6b
            the DRM file-descriptor, all open dumb-buffers are automatically
Packit Service 103f6b
            destroyed. This ioctl takes as argument a structure of type
Packit Service 103f6b
            <structname>struct drm_mode_destroy_dumb</structname>:
Packit Service 103f6b
Packit Service 103f6b
<programlisting>
Packit Service 103f6b
struct drm_mode_destroy_dumb {
Packit Service 103f6b
	__u32 handle;
Packit Service 103f6b
};
Packit Service 103f6b
</programlisting>
Packit Service 103f6b
Packit Service 103f6b
            You only need to put your handle into the
Packit Service 103f6b
            <structfield>handle</structfield> field. After this call, the handle
Packit Service 103f6b
            is invalid and may be reused for new buffers by the dumb-API.</para>
Packit Service 103f6b
Packit Service 103f6b
    </refsect2>
Packit Service 103f6b
Packit Service 103f6b
    <refsect2>
Packit Service 103f6b
      <title>TTM</title>
Packit Service 103f6b
      <para><emphasis>TTM</emphasis> stands for
Packit Service 103f6b
            <emphasis>Translation Table Manager</emphasis> and is a generic
Packit Service 103f6b
            memory-manager provided by the kernel. It does not provide a common
Packit Service 103f6b
            user-space API so you need to look at each driver interface if you
Packit Service 103f6b
            want to use it. See for instance the radeon manpages for more
Packit Service 103f6b
            information on memory-management with radeon and TTM.</para>
Packit Service 103f6b
    </refsect2>
Packit Service 103f6b
Packit Service 103f6b
    <refsect2>
Packit Service 103f6b
      <title>GEM</title>
Packit Service 103f6b
      <para><emphasis>GEM</emphasis> stands for
Packit Service 103f6b
            <emphasis>Graphics Execution Manager</emphasis> and is a generic DRM
Packit Service 103f6b
            memory-management framework in the kernel, that is used by many
Packit Service 103f6b
            different drivers. Gem is designed to manage graphics memory,
Packit Service 103f6b
            control access to the graphics device execution context and handle
Packit Service 103f6b
            essentially NUMA environment unique to modern graphics hardware. Gem
Packit Service 103f6b
            allows multiple applications to share graphics device resources
Packit Service 103f6b
            without the need to constantly reload the entire graphics card. Data
Packit Service 103f6b
            may be shared between multiple applications with gem ensuring that
Packit Service 103f6b
            the correct memory synchronization occurs.</para>
Packit Service 103f6b
Packit Service 103f6b
      <para>Gem provides simple mechanisms to manage graphics data and control
Packit Service 103f6b
            execution flow within the linux DRM subsystem. However, gem is not a
Packit Service 103f6b
            complete framework that is fully driver independent. Instead, if
Packit Service 103f6b
            provides many functions that are shared between many drivers, but
Packit Service 103f6b
            each driver has to implement most of memory-management with
Packit Service 103f6b
            driver-dependent ioctls. This manpage tries to describe the
Packit Service 103f6b
            semantics (and if it applies, the syntax) that is shared between all
Packit Service 103f6b
            drivers that use gem.</para>
Packit Service 103f6b
Packit Service 103f6b
      <para>All GEM APIs are defined as
Packit Service 103f6b
            <citerefentry><refentrytitle>ioctl</refentrytitle><manvolnum>2</manvolnum></citerefentry>
Packit Service 103f6b
            on the DRM file descriptor. An application must be authorized via
Packit Service 103f6b
            <citerefentry><refentrytitle>drmAuthMagic</refentrytitle><manvolnum>3</manvolnum></citerefentry>
Packit Service 103f6b
            to the current DRM-Master to access the GEM subsystem. A driver that
Packit Service 103f6b
            does not support gem will return <constant>ENODEV</constant> for all
Packit Service 103f6b
            these ioctls. Invalid object handles return
Packit Service 103f6b
            <constant>EINVAL</constant> and invalid object names return
Packit Service 103f6b
            <constant>ENOENT</constant>.</para>
Packit Service 103f6b
Packit Service 103f6b
      <para>Gem provides explicit memory management primitives. System pages are
Packit Service 103f6b
            allocated when the object is created, either as the fundamental
Packit Service 103f6b
            storage for hardware where system memory is used by the graphics
Packit Service 103f6b
            processor directly, or as backing store for graphics-processor
Packit Service 103f6b
            resident memory.</para>
Packit Service 103f6b
Packit Service 103f6b
      <para>Objects are referenced from user-space using handles. These are, for
Packit Service 103f6b
            all intents and purposes, equivalent to file descriptors but avoid
Packit Service 103f6b
            the overhead. Newer kernel drivers also support the
Packit Service 103f6b
            <citerefentry><refentrytitle>drm-prime</refentrytitle><manvolnum>7</manvolnum></citerefentry>
Packit Service 103f6b
            infrastructure which can return real file-descriptor for gem-handles
Packit Service 103f6b
            using the linux dma-buf API. Objects may be published with a name so
Packit Service 103f6b
            that other applications and processes can access them. The name
Packit Service 103f6b
            remains valid as long as the object exists. Gem-objects are
Packit Service 103f6b
            reference counted in the kernel. The object is only destroyed when
Packit Service 103f6b
            all handles from user-space were closed.</para>
Packit Service 103f6b
Packit Service 103f6b
      <para>Gem-buffers cannot be created with a generic API. Each driver
Packit Service 103f6b
            provides its own API to create gem-buffers. See for example
Packit Service 103f6b
            <constant>DRM_I915_GEM_CREATE</constant>,
Packit Service 103f6b
            <constant>DRM_NOUVEAU_GEM_NEW</constant> or
Packit Service 103f6b
            <constant>DRM_RADEON_GEM_CREATE</constant>. Each of these ioctls
Packit Service 103f6b
            returns a gem-handle that can be passed to different generic ioctls.
Packit Service 103f6b
            The <emphasis>libgbm</emphasis> library from the
Packit Service 103f6b
            <emphasis>mesa3D</emphasis> distribution tries to provide a
Packit Service 103f6b
            driver-independent API to create gbm buffers and retrieve a
Packit Service 103f6b
            gbm-handle to them. It allows to create buffers for different
Packit Service 103f6b
            use-cases including scanout, rendering, cursors and CPU-access. See
Packit Service 103f6b
            the libgbm library for more information or look at the
Packit Service 103f6b
            driver-dependent man-pages (for example
Packit Service 103f6b
            <citerefentry><refentrytitle>drm-intel</refentrytitle><manvolnum>7</manvolnum></citerefentry>
Packit Service 103f6b
            or
Packit Service 103f6b
            <citerefentry><refentrytitle>drm-radeon</refentrytitle><manvolnum>7</manvolnum></citerefentry>).</para>
Packit Service 103f6b
Packit Service 103f6b
      <para>Gem-buffers can be closed with the
Packit Service 103f6b
            <constant>DRM_IOCTL_GEM_CLOSE</constant> ioctl. It takes as argument
Packit Service 103f6b
            a structure of type <structname>struct drm_gem_close</structname>:
Packit Service 103f6b
Packit Service 103f6b
<programlisting>
Packit Service 103f6b
struct drm_gem_close {
Packit Service 103f6b
	__u32 handle;
Packit Service 103f6b
	__u32 pad;
Packit Service 103f6b
};
Packit Service 103f6b
</programlisting>
Packit Service 103f6b
Packit Service 103f6b
            The <structfield>handle</structfield> field is the gem-handle to be
Packit Service 103f6b
            closed. The <structfield>pad</structfield> field is unused padding.
Packit Service 103f6b
            It must be zeroed. After this call the gem handle cannot be used by
Packit Service 103f6b
            this process anymore and may be reused for new gem objects by the
Packit Service 103f6b
            gem API.</para>
Packit Service 103f6b
Packit Service 103f6b
      <para>If you want to share gem-objects between different processes, you
Packit Service 103f6b
            can create a name for them and pass this name to other processes
Packit Service 103f6b
            which can then open this gem-object. Names are currently 32bit
Packit Service 103f6b
            integer IDs and have no special protection. That is, if you put a
Packit Service 103f6b
            name on your gem-object, every other client that has access to the
Packit Service 103f6b
            DRM device and is authenticated via
Packit Service 103f6b
            <citerefentry><refentrytitle>drmAuthMagic</refentrytitle><manvolnum>3</manvolnum></citerefentry>
Packit Service 103f6b
            to the current DRM-Master, can <emphasis>guess</emphasis> the name
Packit Service 103f6b
            and open or access the gem-object. If you want more fine-grained
Packit Service 103f6b
            access control, you can use the new
Packit Service 103f6b
            <citerefentry><refentrytitle>drm-prime</refentrytitle><manvolnum>7</manvolnum></citerefentry>
Packit Service 103f6b
            API to retrieve file-descriptors for gem-handles. To create a name
Packit Service 103f6b
            for a gem-handle, you use the
Packit Service 103f6b
            <constant>DRM_IOCTL_GEM_FLINK</constant> ioctl. It takes as argument
Packit Service 103f6b
            a structure of type <structname>struct drm_gem_flink</structname>:
Packit Service 103f6b
Packit Service 103f6b
<programlisting>
Packit Service 103f6b
struct drm_gem_flink {
Packit Service 103f6b
	__u32 handle;
Packit Service 103f6b
	__u32 name;
Packit Service 103f6b
};
Packit Service 103f6b
</programlisting>
Packit Service 103f6b
Packit Service 103f6b
            You have to put your handle into the
Packit Service 103f6b
            <structfield>handle</structfield> field. After completion, the
Packit Service 103f6b
            kernel has put the new unique name into the
Packit Service 103f6b
            <structfield>name</structfield> field. You can now pass this name to
Packit Service 103f6b
            other processes which can then import the name with the
Packit Service 103f6b
            <constant>DRM_IOCTL_GEM_OPEN</constant> ioctl. It takes as argument
Packit Service 103f6b
            a structure of type <structname>struct drm_gem_open</structname>:
Packit Service 103f6b
Packit Service 103f6b
<programlisting>
Packit Service 103f6b
struct drm_gem_open {
Packit Service 103f6b
	__u32 name;
Packit Service 103f6b
Packit Service 103f6b
	__u32 handle;
Packit Service 103f6b
	__u32 size;
Packit Service 103f6b
};
Packit Service 103f6b
</programlisting>
Packit Service 103f6b
Packit Service 103f6b
            You have to fill in the <structfield>name</structfield> field with
Packit Service 103f6b
            the name of the gem-object that you want to open. The kernel will
Packit Service 103f6b
            fill in the <structfield>handle</structfield> and
Packit Service 103f6b
            <structfield>size</structfield> fields with the new handle and size
Packit Service 103f6b
            of the gem-object. You can now access the gem-object via the handle
Packit Service 103f6b
            as if you created it with the gem API.</para>
Packit Service 103f6b
Packit Service 103f6b
      <para>Besides generic buffer management, the GEM API does not provide any
Packit Service 103f6b
            generic access. Each driver implements its own functionality on top
Packit Service 103f6b
            of this API. This includes execution-buffers, GTT management,
Packit Service 103f6b
            context creation, CPU access, GPU I/O and more. The next
Packit Service 103f6b
            higher-level API is <emphasis>OpenGL</emphasis>. So if you want to
Packit Service 103f6b
            use more GPU features, you should use the
Packit Service 103f6b
            <emphasis>mesa3D</emphasis> library to create OpenGL contexts on DRM
Packit Service 103f6b
            devices. This does <emphasis>not</emphasis> require any
Packit Service 103f6b
            windowing-system like X11, but can also be done on raw DRM devices.
Packit Service 103f6b
            However, this is beyond the scope of this man-page. You may have a
Packit Service 103f6b
            look at other mesa3D manpages, including libgbm and libEGL. 2D
Packit Service 103f6b
            software-rendering (rendering with the CPU) can be achieved with the
Packit Service 103f6b
            dumb-buffer-API in a driver-independent fashion, however, for
Packit Service 103f6b
            hardware-accelerated 2D or 3D rendering you must use OpenGL. Any
Packit Service 103f6b
            other API that tries to abstract the driver-internals to access
Packit Service 103f6b
            GEM-execution-buffers and other GPU internals, would simply reinvent
Packit Service 103f6b
            OpenGL so it is not provided. But if you need more detailed
Packit Service 103f6b
            information for a specific driver, you may have a look into the
Packit Service 103f6b
            driver-manpages, including
Packit Service 103f6b
            <citerefentry><refentrytitle>drm-intel</refentrytitle><manvolnum>7</manvolnum></citerefentry>,
Packit Service 103f6b
            <citerefentry><refentrytitle>drm-radeon</refentrytitle><manvolnum>7</manvolnum></citerefentry>
Packit Service 103f6b
            and
Packit Service 103f6b
            <citerefentry><refentrytitle>drm-nouveau</refentrytitle><manvolnum>7</manvolnum></citerefentry>.
Packit Service 103f6b
            However, the
Packit Service 103f6b
            <citerefentry><refentrytitle>drm-prime</refentrytitle><manvolnum>7</manvolnum></citerefentry>
Packit Service 103f6b
            infrastructure and the generic gem API as described here allow
Packit Service 103f6b
            display-managers to handle graphics-buffers and render-clients
Packit Service 103f6b
            without any deeper knowledge of the GPU that is used. Moreover, it
Packit Service 103f6b
            allows to move objects between GPUs and implement complex
Packit Service 103f6b
            display-servers that don't do any rendering on their own. See its
Packit Service 103f6b
            man-page for more information.</para>
Packit Service 103f6b
    </refsect2>
Packit Service 103f6b
  </refsect1>
Packit Service 103f6b
Packit Service 103f6b
  <refsect1>
Packit Service 103f6b
    <title>Examples</title>
Packit Service 103f6b
      <para>This section includes examples for basic memory-management
Packit Service 103f6b
            tasks.</para>
Packit Service 103f6b
Packit Service 103f6b
    <refsect2>
Packit Service 103f6b
      <title>Dumb-Buffers</title>
Packit Service 103f6b
        <para>This examples shows how to create a dumb-buffer via the generic
Packit Service 103f6b
              DRM API. This is driver-independent (as long as the driver
Packit Service 103f6b
              supports dumb-buffers) and provides memory-mapped buffers that can
Packit Service 103f6b
              be used for scanout. This example creates a full-HD 1920x1080
Packit Service 103f6b
              buffer with 32 bits-per-pixel and a color-depth of 24 bits. The
Packit Service 103f6b
              buffer is then bound to a framebuffer which can be used for
Packit Service 103f6b
              scanout with the KMS API (see
Packit Service 103f6b
              <citerefentry><refentrytitle>drm-kms</refentrytitle><manvolnum>7</manvolnum></citerefentry>).</para>
Packit Service 103f6b
Packit Service 103f6b
<programlisting>
Packit Service 103f6b
struct drm_mode_create_dumb creq;
Packit Service 103f6b
struct drm_mode_destroy_dumb dreq;
Packit Service 103f6b
struct drm_mode_map_dumb mreq;
Packit Service 103f6b
uint32_t fb;
Packit Service 103f6b
int ret;
Packit Service 103f6b
void *map;
Packit Service 103f6b
Packit Service 103f6b
/* create dumb buffer */
Packit Service 103f6b
memset(&creq, 0, sizeof(creq));
Packit Service 103f6b
creq.width = 1920;
Packit Service 103f6b
creq.height = 1080;
Packit Service 103f6b
creq.bpp = 32;
Packit Service 103f6b
ret = drmIoctl(fd, DRM_IOCTL_MODE_CREATE_DUMB, &creq);
Packit Service 103f6b
if (ret < 0) {
Packit Service 103f6b
	/* buffer creation failed; see "errno" for more error codes */
Packit Service 103f6b
	...
Packit Service 103f6b
}
Packit Service 103f6b
/* creq.pitch, creq.handle and creq.size are filled by this ioctl with
Packit Service 103f6b
 * the requested values and can be used now. */
Packit Service 103f6b
Packit Service 103f6b
/* create framebuffer object for the dumb-buffer */
Packit Service 103f6b
ret = drmModeAddFB(fd, 1920, 1080, 24, 32, creq.pitch, creq.handle, &fb);
Packit Service 103f6b
if (ret) {
Packit Service 103f6b
	/* frame buffer creation failed; see "errno" */
Packit Service 103f6b
	...
Packit Service 103f6b
}
Packit Service 103f6b
/* the framebuffer "fb" can now used for scanout with KMS */
Packit Service 103f6b
Packit Service 103f6b
/* prepare buffer for memory mapping */
Packit Service 103f6b
memset(&mreq, 0, sizeof(mreq));
Packit Service 103f6b
mreq.handle = creq.handle;
Packit Service 103f6b
ret = drmIoctl(fd, DRM_IOCTL_MODE_MAP_DUMB, &mreq);
Packit Service 103f6b
if (ret) {
Packit Service 103f6b
	/* DRM buffer preparation failed; see "errno" */
Packit Service 103f6b
	...
Packit Service 103f6b
}
Packit Service 103f6b
/* mreq.offset now contains the new offset that can be used with mmap() */
Packit Service 103f6b
Packit Service 103f6b
/* perform actual memory mapping */
Packit Service 103f6b
map = mmap(0, creq.size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, mreq.offset);
Packit Service 103f6b
if (map == MAP_FAILED) {
Packit Service 103f6b
	/* memory-mapping failed; see "errno" */
Packit Service 103f6b
	...
Packit Service 103f6b
}
Packit Service 103f6b
Packit Service 103f6b
/* clear the framebuffer to 0 */
Packit Service 103f6b
memset(map, 0, creq.size);
Packit Service 103f6b
</programlisting>
Packit Service 103f6b
Packit Service 103f6b
    </refsect2>
Packit Service 103f6b
Packit Service 103f6b
  </refsect1>
Packit Service 103f6b
Packit Service 103f6b
  <refsect1>
Packit Service 103f6b
    <title>Reporting Bugs</title>
Packit Service 103f6b
    <para>Bugs in this manual should be reported to
Packit Service 103f6b
      https://gitlab.freedesktop.org/mesa/drm/-/issues</para>
Packit Service 103f6b
  </refsect1>
Packit Service 103f6b
Packit Service 103f6b
  <refsect1>
Packit Service 103f6b
    <title>See Also</title>
Packit Service 103f6b
    <para>
Packit Service 103f6b
      <citerefentry><refentrytitle>drm</refentrytitle><manvolnum>7</manvolnum></citerefentry>,
Packit Service 103f6b
      <citerefentry><refentrytitle>drm-kms</refentrytitle><manvolnum>7</manvolnum></citerefentry>,
Packit Service 103f6b
      <citerefentry><refentrytitle>drm-prime</refentrytitle><manvolnum>7</manvolnum></citerefentry>,
Packit Service 103f6b
      <citerefentry><refentrytitle>drmAvailable</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
Packit Service 103f6b
      <citerefentry><refentrytitle>drmOpen</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
Packit Service 103f6b
      <citerefentry><refentrytitle>drm-intel</refentrytitle><manvolnum>7</manvolnum></citerefentry>,
Packit Service 103f6b
      <citerefentry><refentrytitle>drm-radeon</refentrytitle><manvolnum>7</manvolnum></citerefentry>,
Packit Service 103f6b
      <citerefentry><refentrytitle>drm-nouveau</refentrytitle><manvolnum>7</manvolnum></citerefentry>
Packit Service 103f6b
    </para>
Packit Service 103f6b
  </refsect1>
Packit Service 103f6b
</refentry>