sti_cursor.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) STMicroelectronics SA 2014
  4. * Authors: Vincent Abriou <vincent.abriou@st.com>
  5. * Fabien Dessenne <fabien.dessenne@st.com>
  6. * for STMicroelectronics.
  7. */
  8. #include <linux/dma-mapping.h>
  9. #include <linux/seq_file.h>
  10. #include <drm/drm_atomic.h>
  11. #include <drm/drm_device.h>
  12. #include <drm/drm_fb_cma_helper.h>
  13. #include <drm/drm_gem_cma_helper.h>
  14. #include "sti_compositor.h"
  15. #include "sti_cursor.h"
  16. #include "sti_plane.h"
  17. #include "sti_vtg.h"
  18. /* Registers */
  19. #define CUR_CTL 0x00
  20. #define CUR_VPO 0x0C
  21. #define CUR_PML 0x14
  22. #define CUR_PMP 0x18
  23. #define CUR_SIZE 0x1C
  24. #define CUR_CML 0x20
  25. #define CUR_AWS 0x28
  26. #define CUR_AWE 0x2C
  27. #define CUR_CTL_CLUT_UPDATE BIT(1)
  28. #define STI_CURS_MIN_SIZE 1
  29. #define STI_CURS_MAX_SIZE 128
  30. /*
  31. * pixmap dma buffer structure
  32. *
  33. * @paddr: physical address
  34. * @size: buffer size
  35. * @base: virtual address
  36. */
  37. struct dma_pixmap {
  38. dma_addr_t paddr;
  39. size_t size;
  40. void *base;
  41. };
  42. /*
  43. * STI Cursor structure
  44. *
  45. * @sti_plane: sti_plane structure
  46. * @dev: driver device
  47. * @regs: cursor registers
  48. * @width: cursor width
  49. * @height: cursor height
  50. * @clut: color look up table
  51. * @clut_paddr: color look up table physical address
  52. * @pixmap: pixmap dma buffer (clut8-format cursor)
  53. */
  54. struct sti_cursor {
  55. struct sti_plane plane;
  56. struct device *dev;
  57. void __iomem *regs;
  58. unsigned int width;
  59. unsigned int height;
  60. unsigned short *clut;
  61. dma_addr_t clut_paddr;
  62. struct dma_pixmap pixmap;
  63. };
  64. static const uint32_t cursor_supported_formats[] = {
  65. DRM_FORMAT_ARGB8888,
  66. };
  67. #define to_sti_cursor(x) container_of(x, struct sti_cursor, plane)
  68. #define DBGFS_DUMP(reg) seq_printf(s, "\n %-25s 0x%08X", #reg, \
  69. readl(cursor->regs + reg))
  70. static void cursor_dbg_vpo(struct seq_file *s, u32 val)
  71. {
  72. seq_printf(s, "\txdo:%4d\tydo:%4d", val & 0x0FFF, (val >> 16) & 0x0FFF);
  73. }
  74. static void cursor_dbg_size(struct seq_file *s, u32 val)
  75. {
  76. seq_printf(s, "\t%d x %d", val & 0x07FF, (val >> 16) & 0x07FF);
  77. }
  78. static void cursor_dbg_pml(struct seq_file *s,
  79. struct sti_cursor *cursor, u32 val)
  80. {
  81. if (cursor->pixmap.paddr == val)
  82. seq_printf(s, "\tVirt @: %p", cursor->pixmap.base);
  83. }
  84. static void cursor_dbg_cml(struct seq_file *s,
  85. struct sti_cursor *cursor, u32 val)
  86. {
  87. if (cursor->clut_paddr == val)
  88. seq_printf(s, "\tVirt @: %p", cursor->clut);
  89. }
  90. static int cursor_dbg_show(struct seq_file *s, void *data)
  91. {
  92. struct drm_info_node *node = s->private;
  93. struct sti_cursor *cursor = (struct sti_cursor *)node->info_ent->data;
  94. seq_printf(s, "%s: (vaddr = 0x%p)",
  95. sti_plane_to_str(&cursor->plane), cursor->regs);
  96. DBGFS_DUMP(CUR_CTL);
  97. DBGFS_DUMP(CUR_VPO);
  98. cursor_dbg_vpo(s, readl(cursor->regs + CUR_VPO));
  99. DBGFS_DUMP(CUR_PML);
  100. cursor_dbg_pml(s, cursor, readl(cursor->regs + CUR_PML));
  101. DBGFS_DUMP(CUR_PMP);
  102. DBGFS_DUMP(CUR_SIZE);
  103. cursor_dbg_size(s, readl(cursor->regs + CUR_SIZE));
  104. DBGFS_DUMP(CUR_CML);
  105. cursor_dbg_cml(s, cursor, readl(cursor->regs + CUR_CML));
  106. DBGFS_DUMP(CUR_AWS);
  107. DBGFS_DUMP(CUR_AWE);
  108. seq_putc(s, '\n');
  109. return 0;
  110. }
  111. static struct drm_info_list cursor_debugfs_files[] = {
  112. { "cursor", cursor_dbg_show, 0, NULL },
  113. };
  114. static void cursor_debugfs_init(struct sti_cursor *cursor,
  115. struct drm_minor *minor)
  116. {
  117. unsigned int i;
  118. for (i = 0; i < ARRAY_SIZE(cursor_debugfs_files); i++)
  119. cursor_debugfs_files[i].data = cursor;
  120. drm_debugfs_create_files(cursor_debugfs_files,
  121. ARRAY_SIZE(cursor_debugfs_files),
  122. minor->debugfs_root, minor);
  123. }
  124. static void sti_cursor_argb8888_to_clut8(struct sti_cursor *cursor, u32 *src)
  125. {
  126. u8 *dst = cursor->pixmap.base;
  127. unsigned int i, j;
  128. u32 a, r, g, b;
  129. for (i = 0; i < cursor->height; i++) {
  130. for (j = 0; j < cursor->width; j++) {
  131. /* Pick the 2 higher bits of each component */
  132. a = (*src >> 30) & 3;
  133. r = (*src >> 22) & 3;
  134. g = (*src >> 14) & 3;
  135. b = (*src >> 6) & 3;
  136. *dst = a << 6 | r << 4 | g << 2 | b;
  137. src++;
  138. dst++;
  139. }
  140. }
  141. }
  142. static void sti_cursor_init(struct sti_cursor *cursor)
  143. {
  144. unsigned short *base = cursor->clut;
  145. unsigned int a, r, g, b;
  146. /* Assign CLUT values, ARGB444 format */
  147. for (a = 0; a < 4; a++)
  148. for (r = 0; r < 4; r++)
  149. for (g = 0; g < 4; g++)
  150. for (b = 0; b < 4; b++)
  151. *base++ = (a * 5) << 12 |
  152. (r * 5) << 8 |
  153. (g * 5) << 4 |
  154. (b * 5);
  155. }
  156. static int sti_cursor_atomic_check(struct drm_plane *drm_plane,
  157. struct drm_plane_state *state)
  158. {
  159. struct sti_plane *plane = to_sti_plane(drm_plane);
  160. struct sti_cursor *cursor = to_sti_cursor(plane);
  161. struct drm_crtc *crtc = state->crtc;
  162. struct drm_framebuffer *fb = state->fb;
  163. struct drm_crtc_state *crtc_state;
  164. struct drm_display_mode *mode;
  165. int dst_x, dst_y, dst_w, dst_h;
  166. int src_w, src_h;
  167. /* no need for further checks if the plane is being disabled */
  168. if (!crtc || !fb)
  169. return 0;
  170. crtc_state = drm_atomic_get_crtc_state(state->state, crtc);
  171. mode = &crtc_state->mode;
  172. dst_x = state->crtc_x;
  173. dst_y = state->crtc_y;
  174. dst_w = clamp_val(state->crtc_w, 0, mode->crtc_hdisplay - dst_x);
  175. dst_h = clamp_val(state->crtc_h, 0, mode->crtc_vdisplay - dst_y);
  176. /* src_x are in 16.16 format */
  177. src_w = state->src_w >> 16;
  178. src_h = state->src_h >> 16;
  179. if (src_w < STI_CURS_MIN_SIZE ||
  180. src_h < STI_CURS_MIN_SIZE ||
  181. src_w > STI_CURS_MAX_SIZE ||
  182. src_h > STI_CURS_MAX_SIZE) {
  183. DRM_ERROR("Invalid cursor size (%dx%d)\n",
  184. src_w, src_h);
  185. return -EINVAL;
  186. }
  187. /* If the cursor size has changed, re-allocated the pixmap */
  188. if (!cursor->pixmap.base ||
  189. (cursor->width != src_w) ||
  190. (cursor->height != src_h)) {
  191. cursor->width = src_w;
  192. cursor->height = src_h;
  193. if (cursor->pixmap.base)
  194. dma_free_wc(cursor->dev, cursor->pixmap.size,
  195. cursor->pixmap.base, cursor->pixmap.paddr);
  196. cursor->pixmap.size = cursor->width * cursor->height;
  197. cursor->pixmap.base = dma_alloc_wc(cursor->dev,
  198. cursor->pixmap.size,
  199. &cursor->pixmap.paddr,
  200. GFP_KERNEL | GFP_DMA);
  201. if (!cursor->pixmap.base) {
  202. DRM_ERROR("Failed to allocate memory for pixmap\n");
  203. return -EINVAL;
  204. }
  205. }
  206. if (!drm_fb_cma_get_gem_obj(fb, 0)) {
  207. DRM_ERROR("Can't get CMA GEM object for fb\n");
  208. return -EINVAL;
  209. }
  210. DRM_DEBUG_KMS("CRTC:%d (%s) drm plane:%d (%s)\n",
  211. crtc->base.id, sti_mixer_to_str(to_sti_mixer(crtc)),
  212. drm_plane->base.id, sti_plane_to_str(plane));
  213. DRM_DEBUG_KMS("(%dx%d)@(%d,%d)\n", dst_w, dst_h, dst_x, dst_y);
  214. return 0;
  215. }
  216. static void sti_cursor_atomic_update(struct drm_plane *drm_plane,
  217. struct drm_plane_state *oldstate)
  218. {
  219. struct drm_plane_state *state = drm_plane->state;
  220. struct sti_plane *plane = to_sti_plane(drm_plane);
  221. struct sti_cursor *cursor = to_sti_cursor(plane);
  222. struct drm_crtc *crtc = state->crtc;
  223. struct drm_framebuffer *fb = state->fb;
  224. struct drm_display_mode *mode;
  225. int dst_x, dst_y;
  226. struct drm_gem_cma_object *cma_obj;
  227. u32 y, x;
  228. u32 val;
  229. if (!crtc || !fb)
  230. return;
  231. mode = &crtc->mode;
  232. dst_x = state->crtc_x;
  233. dst_y = state->crtc_y;
  234. cma_obj = drm_fb_cma_get_gem_obj(fb, 0);
  235. /* Convert ARGB8888 to CLUT8 */
  236. sti_cursor_argb8888_to_clut8(cursor, (u32 *)cma_obj->vaddr);
  237. /* AWS and AWE depend on the mode */
  238. y = sti_vtg_get_line_number(*mode, 0);
  239. x = sti_vtg_get_pixel_number(*mode, 0);
  240. val = y << 16 | x;
  241. writel(val, cursor->regs + CUR_AWS);
  242. y = sti_vtg_get_line_number(*mode, mode->vdisplay - 1);
  243. x = sti_vtg_get_pixel_number(*mode, mode->hdisplay - 1);
  244. val = y << 16 | x;
  245. writel(val, cursor->regs + CUR_AWE);
  246. /* Set memory location, size, and position */
  247. writel(cursor->pixmap.paddr, cursor->regs + CUR_PML);
  248. writel(cursor->width, cursor->regs + CUR_PMP);
  249. writel(cursor->height << 16 | cursor->width, cursor->regs + CUR_SIZE);
  250. y = sti_vtg_get_line_number(*mode, dst_y);
  251. x = sti_vtg_get_pixel_number(*mode, dst_x);
  252. writel((y << 16) | x, cursor->regs + CUR_VPO);
  253. /* Set and fetch CLUT */
  254. writel(cursor->clut_paddr, cursor->regs + CUR_CML);
  255. writel(CUR_CTL_CLUT_UPDATE, cursor->regs + CUR_CTL);
  256. sti_plane_update_fps(plane, true, false);
  257. plane->status = STI_PLANE_UPDATED;
  258. }
  259. static void sti_cursor_atomic_disable(struct drm_plane *drm_plane,
  260. struct drm_plane_state *oldstate)
  261. {
  262. struct sti_plane *plane = to_sti_plane(drm_plane);
  263. if (!oldstate->crtc) {
  264. DRM_DEBUG_DRIVER("drm plane:%d not enabled\n",
  265. drm_plane->base.id);
  266. return;
  267. }
  268. DRM_DEBUG_DRIVER("CRTC:%d (%s) drm plane:%d (%s)\n",
  269. oldstate->crtc->base.id,
  270. sti_mixer_to_str(to_sti_mixer(oldstate->crtc)),
  271. drm_plane->base.id, sti_plane_to_str(plane));
  272. plane->status = STI_PLANE_DISABLING;
  273. }
  274. static const struct drm_plane_helper_funcs sti_cursor_helpers_funcs = {
  275. .atomic_check = sti_cursor_atomic_check,
  276. .atomic_update = sti_cursor_atomic_update,
  277. .atomic_disable = sti_cursor_atomic_disable,
  278. };
  279. static void sti_cursor_destroy(struct drm_plane *drm_plane)
  280. {
  281. DRM_DEBUG_DRIVER("\n");
  282. drm_plane_cleanup(drm_plane);
  283. }
  284. static int sti_cursor_late_register(struct drm_plane *drm_plane)
  285. {
  286. struct sti_plane *plane = to_sti_plane(drm_plane);
  287. struct sti_cursor *cursor = to_sti_cursor(plane);
  288. cursor_debugfs_init(cursor, drm_plane->dev->primary);
  289. return 0;
  290. }
  291. static const struct drm_plane_funcs sti_cursor_plane_helpers_funcs = {
  292. .update_plane = drm_atomic_helper_update_plane,
  293. .disable_plane = drm_atomic_helper_disable_plane,
  294. .destroy = sti_cursor_destroy,
  295. .reset = sti_plane_reset,
  296. .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
  297. .atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
  298. .late_register = sti_cursor_late_register,
  299. };
  300. struct drm_plane *sti_cursor_create(struct drm_device *drm_dev,
  301. struct device *dev, int desc,
  302. void __iomem *baseaddr,
  303. unsigned int possible_crtcs)
  304. {
  305. struct sti_cursor *cursor;
  306. size_t size;
  307. int res;
  308. cursor = devm_kzalloc(dev, sizeof(*cursor), GFP_KERNEL);
  309. if (!cursor) {
  310. DRM_ERROR("Failed to allocate memory for cursor\n");
  311. return NULL;
  312. }
  313. /* Allocate clut buffer */
  314. size = 0x100 * sizeof(unsigned short);
  315. cursor->clut = dma_alloc_wc(dev, size, &cursor->clut_paddr,
  316. GFP_KERNEL | GFP_DMA);
  317. if (!cursor->clut) {
  318. DRM_ERROR("Failed to allocate memory for cursor clut\n");
  319. goto err_clut;
  320. }
  321. cursor->dev = dev;
  322. cursor->regs = baseaddr;
  323. cursor->plane.desc = desc;
  324. cursor->plane.status = STI_PLANE_DISABLED;
  325. sti_cursor_init(cursor);
  326. res = drm_universal_plane_init(drm_dev, &cursor->plane.drm_plane,
  327. possible_crtcs,
  328. &sti_cursor_plane_helpers_funcs,
  329. cursor_supported_formats,
  330. ARRAY_SIZE(cursor_supported_formats),
  331. NULL, DRM_PLANE_TYPE_CURSOR, NULL);
  332. if (res) {
  333. DRM_ERROR("Failed to initialize universal plane\n");
  334. goto err_plane;
  335. }
  336. drm_plane_helper_add(&cursor->plane.drm_plane,
  337. &sti_cursor_helpers_funcs);
  338. sti_plane_init_property(&cursor->plane, DRM_PLANE_TYPE_CURSOR);
  339. return &cursor->plane.drm_plane;
  340. err_plane:
  341. dma_free_wc(dev, size, cursor->clut, cursor->clut_paddr);
  342. err_clut:
  343. devm_kfree(dev, cursor);
  344. return NULL;
  345. }