sti_cursor.c 11 KB

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