xref: /openbmc/linux/drivers/gpu/drm/gud/gud_internal.h (revision c616fb0c)
1 /* SPDX-License-Identifier: MIT */
2 
3 #ifndef __LINUX_GUD_INTERNAL_H
4 #define __LINUX_GUD_INTERNAL_H
5 
6 #include <linux/list.h>
7 #include <linux/mutex.h>
8 #include <linux/scatterlist.h>
9 #include <linux/usb.h>
10 #include <linux/workqueue.h>
11 #include <uapi/drm/drm_fourcc.h>
12 
13 #include <drm/drm_modes.h>
14 #include <drm/drm_simple_kms_helper.h>
15 
16 struct gud_device {
17 	struct drm_device drm;
18 	struct drm_simple_display_pipe pipe;
19 	struct device *dmadev;
20 	struct work_struct work;
21 	u32 flags;
22 	const struct drm_format_info *xrgb8888_emulation_format;
23 
24 	u16 *properties;
25 	unsigned int num_properties;
26 
27 	unsigned int bulk_pipe;
28 	void *bulk_buf;
29 	size_t bulk_len;
30 	struct sg_table bulk_sgt;
31 
32 	u8 compression;
33 	void *lz4_comp_mem;
34 	void *compress_buf;
35 
36 	u64 stats_length;
37 	u64 stats_actual_length;
38 	unsigned int stats_num_errors;
39 
40 	struct mutex ctrl_lock; /* Serialize get/set and status transfers */
41 
42 	struct mutex damage_lock; /* Protects the following members: */
43 	struct drm_framebuffer *fb;
44 	struct drm_rect damage;
45 	bool prev_flush_failed;
46 	void *shadow_buf;
47 };
48 
49 static inline struct gud_device *to_gud_device(struct drm_device *drm)
50 {
51 	return container_of(drm, struct gud_device, drm);
52 }
53 
54 static inline struct usb_device *gud_to_usb_device(struct gud_device *gdrm)
55 {
56 	return interface_to_usbdev(to_usb_interface(gdrm->drm.dev));
57 }
58 
59 int gud_usb_get(struct gud_device *gdrm, u8 request, u16 index, void *buf, size_t len);
60 int gud_usb_set(struct gud_device *gdrm, u8 request, u16 index, void *buf, size_t len);
61 int gud_usb_get_u8(struct gud_device *gdrm, u8 request, u16 index, u8 *val);
62 int gud_usb_set_u8(struct gud_device *gdrm, u8 request, u8 val);
63 
64 void gud_clear_damage(struct gud_device *gdrm);
65 void gud_flush_work(struct work_struct *work);
66 int gud_pipe_check(struct drm_simple_display_pipe *pipe,
67 		   struct drm_plane_state *new_plane_state,
68 		   struct drm_crtc_state *new_crtc_state);
69 void gud_pipe_update(struct drm_simple_display_pipe *pipe,
70 		     struct drm_plane_state *old_state);
71 int gud_connector_fill_properties(struct drm_connector_state *connector_state,
72 				  struct gud_property_req *properties);
73 int gud_get_connectors(struct gud_device *gdrm);
74 
75 /* Driver internal fourcc transfer formats */
76 #define GUD_DRM_FORMAT_R1		0x00000122
77 #define GUD_DRM_FORMAT_XRGB1111		0x03121722
78 
79 static inline u8 gud_from_fourcc(u32 fourcc)
80 {
81 	switch (fourcc) {
82 	case GUD_DRM_FORMAT_R1:
83 		return GUD_PIXEL_FORMAT_R1;
84 	case DRM_FORMAT_R8:
85 		return GUD_PIXEL_FORMAT_R8;
86 	case GUD_DRM_FORMAT_XRGB1111:
87 		return GUD_PIXEL_FORMAT_XRGB1111;
88 	case DRM_FORMAT_RGB332:
89 		return GUD_PIXEL_FORMAT_RGB332;
90 	case DRM_FORMAT_RGB565:
91 		return GUD_PIXEL_FORMAT_RGB565;
92 	case DRM_FORMAT_RGB888:
93 		return GUD_PIXEL_FORMAT_RGB888;
94 	case DRM_FORMAT_XRGB8888:
95 		return GUD_PIXEL_FORMAT_XRGB8888;
96 	case DRM_FORMAT_ARGB8888:
97 		return GUD_PIXEL_FORMAT_ARGB8888;
98 	}
99 
100 	return 0;
101 }
102 
103 static inline u32 gud_to_fourcc(u8 format)
104 {
105 	switch (format) {
106 	case GUD_PIXEL_FORMAT_R1:
107 		return GUD_DRM_FORMAT_R1;
108 	case GUD_PIXEL_FORMAT_R8:
109 		return DRM_FORMAT_R8;
110 	case GUD_PIXEL_FORMAT_XRGB1111:
111 		return GUD_DRM_FORMAT_XRGB1111;
112 	case GUD_PIXEL_FORMAT_RGB332:
113 		return DRM_FORMAT_RGB332;
114 	case GUD_PIXEL_FORMAT_RGB565:
115 		return DRM_FORMAT_RGB565;
116 	case GUD_PIXEL_FORMAT_RGB888:
117 		return DRM_FORMAT_RGB888;
118 	case GUD_PIXEL_FORMAT_XRGB8888:
119 		return DRM_FORMAT_XRGB8888;
120 	case GUD_PIXEL_FORMAT_ARGB8888:
121 		return DRM_FORMAT_ARGB8888;
122 	}
123 
124 	return 0;
125 }
126 
127 static inline void gud_from_display_mode(struct gud_display_mode_req *dst,
128 					 const struct drm_display_mode *src)
129 {
130 	u32 flags = src->flags & GUD_DISPLAY_MODE_FLAG_USER_MASK;
131 
132 	if (src->type & DRM_MODE_TYPE_PREFERRED)
133 		flags |= GUD_DISPLAY_MODE_FLAG_PREFERRED;
134 
135 	dst->clock = cpu_to_le32(src->clock);
136 	dst->hdisplay = cpu_to_le16(src->hdisplay);
137 	dst->hsync_start = cpu_to_le16(src->hsync_start);
138 	dst->hsync_end = cpu_to_le16(src->hsync_end);
139 	dst->htotal = cpu_to_le16(src->htotal);
140 	dst->vdisplay = cpu_to_le16(src->vdisplay);
141 	dst->vsync_start = cpu_to_le16(src->vsync_start);
142 	dst->vsync_end = cpu_to_le16(src->vsync_end);
143 	dst->vtotal = cpu_to_le16(src->vtotal);
144 	dst->flags = cpu_to_le32(flags);
145 }
146 
147 static inline void gud_to_display_mode(struct drm_display_mode *dst,
148 				       const struct gud_display_mode_req *src)
149 {
150 	u32 flags = le32_to_cpu(src->flags);
151 
152 	memset(dst, 0, sizeof(*dst));
153 	dst->clock = le32_to_cpu(src->clock);
154 	dst->hdisplay = le16_to_cpu(src->hdisplay);
155 	dst->hsync_start = le16_to_cpu(src->hsync_start);
156 	dst->hsync_end = le16_to_cpu(src->hsync_end);
157 	dst->htotal = le16_to_cpu(src->htotal);
158 	dst->vdisplay = le16_to_cpu(src->vdisplay);
159 	dst->vsync_start = le16_to_cpu(src->vsync_start);
160 	dst->vsync_end = le16_to_cpu(src->vsync_end);
161 	dst->vtotal = le16_to_cpu(src->vtotal);
162 	dst->flags = flags & GUD_DISPLAY_MODE_FLAG_USER_MASK;
163 	dst->type = DRM_MODE_TYPE_DRIVER;
164 	if (flags & GUD_DISPLAY_MODE_FLAG_PREFERRED)
165 		dst->type |= DRM_MODE_TYPE_PREFERRED;
166 	drm_mode_set_name(dst);
167 }
168 
169 #endif
170