sti_cursor.c 10 KB

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