sti_cursor.c 11 KB

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