Libdrm full analysis seven - full source code analysis (4)

Continued from the previous article: Full analysis of libdrm six - full analysis of source code (3)

This article refers to the following blog posts:

DRM driver development (VKMS)

Thank you very much!

This article continues to explain the actual function macro definition in include/drm/drm.h.

5. DRM_IOCTL_GET_MAP

The fifth macro is DRM_IOCTL_GET_MAP, the corresponding code is as follows:

#define DRM_IOCTL_GET_MAP               DRM_IOWR(0x04, struct drm_map)

Combined with the final definition of _IOWR(type,nr,size) in the previous article, the following code is obtained:

#define DRM_IOCTL_GET_MAP		( ((3)  << 30) | (('d') << 8) | ((0x04)   << 0) | ((sizeof(struct drm_map)) << 16) )

struct drm_map is defined in the same file (include/drm/drm.h), the code is as follows:

/*
 * DRM_IOCTL_GET_MAP, DRM_IOCTL_ADD_MAP and DRM_IOCTL_RM_MAP ioctls
 * argument type.
 *
 * \sa drmAddMap().
 */
struct drm_map {
	unsigned long offset;	 /**< Requested physical address (0 for SAREA)*/
	unsigned long size;	 /**< Requested physical size (bytes) */
	enum drm_map_type type;	 /**< Type of memory to map */
	enum drm_map_flags flags;	 /**< Flags */
	void *handle;		 /**< User-space: "Handle" to pass to mmap() */
				 /**< Kernel-space: kernel-virtual address */
	int mtrr;		 /**< MTRR slot used */
	/*   Private data */
};

The definitions of drm_map_type and drm_map_flags are on the top, as follows:

/*
 * Type of memory to map.
 */
enum drm_map_type {
	_DRM_FRAME_BUFFER = 0,	  /**< WC (no caching), no core dump */
	_DRM_REGISTERS = 1,	  /**< no caching, no core dump */
	_DRM_SHM = 2,		  /**< shared, cached */
	_DRM_AGP = 3,		  /**< AGP/GART */
	_DRM_SCATTER_GATHER = 4,  /**< Scatter/gather memory for PCI DMA */
	_DRM_CONSISTENT = 5	  /**< Consistent memory for PCI DMA */
};

/*
 * Memory mapping flags.
 */
enum drm_map_flags {
	_DRM_RESTRICTED = 0x01,	     /**< Cannot be mapped to user-virtual */
	_DRM_READ_ONLY = 0x02,
	_DRM_LOCKED = 0x04,	     /**< shared, cached, locked */
	_DRM_KERNEL = 0x08,	     /**< kernel requires access */
	_DRM_WRITE_COMBINING = 0x10, /**< use write-combining if available */
	_DRM_CONTAINS_LOCK = 0x20,   /**< SHM page that contains lock */
	_DRM_REMOVABLE = 0x40,	     /**< Removable mapping */
	_DRM_DRIVER = 0x80	     /**< Managed by driver */
};

The meaning code comments of each member in the drm_map structure are clearly described, so there is no need to repeat them here.

The Userspace API corresponding to DRM_IOCTL_GET_MAP is: drmGetMap(). This function is also in xf86drm.c, the code is as follows:

drm_public int drmGetMap(int fd, int idx, drm_handle_t *offset, drmSize *size,
                         drmMapType *type, drmMapFlags *flags,
                         drm_handle_t *handle, int *mtrr)
{
    drm_map_t map;

    memclear(map);
    map.offset = idx;
    if (drmIoctl(fd, DRM_IOCTL_GET_MAP, &map))
        return -errno;
    *offset = map.offset;
    *size   = map.size;
    *type   = (drmMapType)map.type;
    *flags  = (drmMapFlags)map.flags;
    *handle = (unsigned long)map.handle;
    *mtrr   = map.mtrr;
    return 0;
}

The role of the function is to obtain information about the memory map (map). This function will be analyzed in detail later when the function is explained in detail. Let's get to know each other first and have an impression.

6. DRM_IOCTL_GET_CLIENT

The sixth macro is DRM_IOCTL_GET_CLIENT, the corresponding code is as follows:

#define DRM_IOCTL_GET_CLIENT            DRM_IOWR(0x05, struct drm_client)

Combined with the final definition of _IOWR(type,nr,size) in the previous article, the following code is obtained:

#define DRM_IOCTL_GET_CLIENT		( ((3)  << 30) | (('d') << 8) | ((0x05)   << 0) | ((sizeof(struct drm_client)) << 16) )

struct drm_client is defined in the same file (include/drm/drm.h), the code is as follows:

/*
 * DRM_IOCTL_GET_CLIENT ioctl argument type.
 */
struct drm_client {
	int idx;		/**< Which client desired? */
	int auth;		/**< Is client authenticated? */
	unsigned long pid;	/**< Process ID */
	unsigned long uid;	/**< User ID */
	unsigned long magic;	/**< Magic */
	unsigned long iocs;	/**< Ioctl count */
};

The meaning code comments of each member in the drm_client structure are clearly described, so there is no need to repeat them here.

The Userspace API corresponding to DRM_IOCTL_GET_CLIENT is: drmGetClient(). This function is also in xf86drm.c, the code is as follows:

drm_public int drmGetClient(int fd, int idx, int *auth, int *pid, int *uid,
                            unsigned long *magic, unsigned long *iocs)
{
    drm_client_t client;

    memclear(client);
    client.idx = idx;
    if (drmIoctl(fd, DRM_IOCTL_GET_CLIENT, &client))
        return -errno;
    *auth      = client.auth;
    *pid       = client.pid;
    *uid       = client.uid;
    *magic     = client.magic;
    *iocs      = client.iocs;
    return 0;
}

The function of the function is to obtain all client processes on the current DRM device. This function will be analyzed in detail later when the function is explained in detail. Let's get to know each other first and have an impression.

The remaining macro definitions will continue to be analyzed in subsequent articles.

Guess you like

Origin blog.csdn.net/phmatthaus/article/details/132468240