lsdc_plane.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2023 Loongson Technology Corporation Limited
  4. */
  5. #include <linux/delay.h>
  6. #include <drm/drm_atomic.h>
  7. #include <drm/drm_atomic_helper.h>
  8. #include <drm/drm_framebuffer.h>
  9. #include <drm/drm_gem_atomic_helper.h>
  10. #include <drm/drm_print.h>
  11. #include "lsdc_drv.h"
  12. #include "lsdc_regs.h"
  13. #include "lsdc_ttm.h"
  14. static const u32 lsdc_primary_formats[] = {
  15. DRM_FORMAT_XRGB8888,
  16. };
  17. static const u32 lsdc_cursor_formats[] = {
  18. DRM_FORMAT_ARGB8888,
  19. };
  20. static const u64 lsdc_fb_format_modifiers[] = {
  21. DRM_FORMAT_MOD_LINEAR,
  22. DRM_FORMAT_MOD_INVALID
  23. };
  24. static unsigned int lsdc_get_fb_offset(struct drm_framebuffer *fb,
  25. struct drm_plane_state *state)
  26. {
  27. unsigned int offset = fb->offsets[0];
  28. offset += fb->format->cpp[0] * (state->src_x >> 16);
  29. offset += fb->pitches[0] * (state->src_y >> 16);
  30. return offset;
  31. }
  32. static u64 lsdc_fb_base_addr(struct drm_framebuffer *fb)
  33. {
  34. struct lsdc_device *ldev = to_lsdc(fb->dev);
  35. struct lsdc_bo *lbo = gem_to_lsdc_bo(fb->obj[0]);
  36. return lsdc_bo_gpu_offset(lbo) + ldev->vram_base;
  37. }
  38. static int lsdc_primary_atomic_check(struct drm_plane *plane,
  39. struct drm_atomic_state *state)
  40. {
  41. struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(state, plane);
  42. struct drm_crtc *crtc = new_plane_state->crtc;
  43. struct drm_crtc_state *new_crtc_state;
  44. if (!crtc)
  45. return 0;
  46. new_crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
  47. return drm_atomic_helper_check_plane_state(new_plane_state,
  48. new_crtc_state,
  49. DRM_PLANE_NO_SCALING,
  50. DRM_PLANE_NO_SCALING,
  51. false, true);
  52. }
  53. static void lsdc_primary_atomic_update(struct drm_plane *plane,
  54. struct drm_atomic_state *state)
  55. {
  56. struct lsdc_primary *primary = to_lsdc_primary(plane);
  57. const struct lsdc_primary_plane_ops *ops = primary->ops;
  58. struct drm_plane_state *old_plane_state = drm_atomic_get_old_plane_state(state, plane);
  59. struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(state, plane);
  60. struct drm_framebuffer *new_fb = new_plane_state->fb;
  61. struct drm_framebuffer *old_fb = old_plane_state->fb;
  62. u64 fb_addr = lsdc_fb_base_addr(new_fb);
  63. fb_addr += lsdc_get_fb_offset(new_fb, new_plane_state);
  64. ops->update_fb_addr(primary, fb_addr);
  65. ops->update_fb_stride(primary, new_fb->pitches[0]);
  66. if (!old_fb || old_fb->format != new_fb->format)
  67. ops->update_fb_format(primary, new_fb->format);
  68. }
  69. static void lsdc_primary_atomic_disable(struct drm_plane *plane,
  70. struct drm_atomic_state *state)
  71. {
  72. /*
  73. * Do nothing, just prevent call into atomic_update().
  74. * Writing the format as LSDC_PF_NONE can disable the primary,
  75. * But it seems not necessary...
  76. */
  77. drm_dbg(plane->dev, "%s disabled\n", plane->name);
  78. }
  79. static int lsdc_plane_prepare_fb(struct drm_plane *plane,
  80. struct drm_plane_state *new_state)
  81. {
  82. struct drm_framebuffer *fb = new_state->fb;
  83. struct lsdc_bo *lbo;
  84. u64 gpu_vaddr;
  85. int ret;
  86. if (!fb)
  87. return 0;
  88. lbo = gem_to_lsdc_bo(fb->obj[0]);
  89. ret = lsdc_bo_reserve(lbo);
  90. if (unlikely(ret)) {
  91. drm_err(plane->dev, "bo %p reserve failed\n", lbo);
  92. return ret;
  93. }
  94. ret = lsdc_bo_pin(lbo, LSDC_GEM_DOMAIN_VRAM, &gpu_vaddr);
  95. lsdc_bo_unreserve(lbo);
  96. if (unlikely(ret)) {
  97. drm_err(plane->dev, "bo %p pin failed\n", lbo);
  98. return ret;
  99. }
  100. lsdc_bo_ref(lbo);
  101. if (plane->type != DRM_PLANE_TYPE_CURSOR)
  102. drm_dbg(plane->dev,
  103. "%s[%p] pin at 0x%llx, bo size: %zu\n",
  104. plane->name, lbo, gpu_vaddr, lsdc_bo_size(lbo));
  105. return drm_gem_plane_helper_prepare_fb(plane, new_state);
  106. }
  107. static void lsdc_plane_cleanup_fb(struct drm_plane *plane,
  108. struct drm_plane_state *old_state)
  109. {
  110. struct drm_framebuffer *fb = old_state->fb;
  111. struct lsdc_bo *lbo;
  112. int ret;
  113. if (!fb)
  114. return;
  115. lbo = gem_to_lsdc_bo(fb->obj[0]);
  116. ret = lsdc_bo_reserve(lbo);
  117. if (unlikely(ret)) {
  118. drm_err(plane->dev, "%p reserve failed\n", lbo);
  119. return;
  120. }
  121. lsdc_bo_unpin(lbo);
  122. lsdc_bo_unreserve(lbo);
  123. lsdc_bo_unref(lbo);
  124. if (plane->type != DRM_PLANE_TYPE_CURSOR)
  125. drm_dbg(plane->dev, "%s unpin\n", plane->name);
  126. }
  127. static const struct drm_plane_helper_funcs lsdc_primary_helper_funcs = {
  128. .prepare_fb = lsdc_plane_prepare_fb,
  129. .cleanup_fb = lsdc_plane_cleanup_fb,
  130. .atomic_check = lsdc_primary_atomic_check,
  131. .atomic_update = lsdc_primary_atomic_update,
  132. .atomic_disable = lsdc_primary_atomic_disable,
  133. };
  134. static int lsdc_cursor_plane_atomic_async_check(struct drm_plane *plane,
  135. struct drm_atomic_state *state,
  136. bool flip)
  137. {
  138. struct drm_plane_state *new_state;
  139. struct drm_crtc_state *crtc_state;
  140. new_state = drm_atomic_get_new_plane_state(state, plane);
  141. if (!plane->state || !plane->state->fb) {
  142. drm_dbg(plane->dev, "%s: state is NULL\n", plane->name);
  143. return -EINVAL;
  144. }
  145. if (new_state->crtc_w != new_state->crtc_h) {
  146. drm_dbg(plane->dev, "unsupported cursor size: %ux%u\n",
  147. new_state->crtc_w, new_state->crtc_h);
  148. return -EINVAL;
  149. }
  150. if (new_state->crtc_w != 64 && new_state->crtc_w != 32) {
  151. drm_dbg(plane->dev, "unsupported cursor size: %ux%u\n",
  152. new_state->crtc_w, new_state->crtc_h);
  153. return -EINVAL;
  154. }
  155. crtc_state = drm_atomic_get_new_crtc_state(state, new_state->crtc);
  156. if (!crtc_state->active)
  157. return -EINVAL;
  158. if (plane->state->crtc != new_state->crtc ||
  159. plane->state->src_w != new_state->src_w ||
  160. plane->state->src_h != new_state->src_h ||
  161. plane->state->crtc_w != new_state->crtc_w ||
  162. plane->state->crtc_h != new_state->crtc_h)
  163. return -EINVAL;
  164. if (new_state->visible != plane->state->visible)
  165. return -EINVAL;
  166. return drm_atomic_helper_check_plane_state(plane->state,
  167. crtc_state,
  168. DRM_PLANE_NO_SCALING,
  169. DRM_PLANE_NO_SCALING,
  170. true, true);
  171. }
  172. static void lsdc_cursor_plane_atomic_async_update(struct drm_plane *plane,
  173. struct drm_atomic_state *state)
  174. {
  175. struct lsdc_cursor *cursor = to_lsdc_cursor(plane);
  176. const struct lsdc_cursor_plane_ops *ops = cursor->ops;
  177. struct drm_framebuffer *old_fb = plane->state->fb;
  178. struct drm_framebuffer *new_fb;
  179. struct drm_plane_state *new_state;
  180. new_state = drm_atomic_get_new_plane_state(state, plane);
  181. new_fb = plane->state->fb;
  182. plane->state->crtc_x = new_state->crtc_x;
  183. plane->state->crtc_y = new_state->crtc_y;
  184. plane->state->crtc_h = new_state->crtc_h;
  185. plane->state->crtc_w = new_state->crtc_w;
  186. plane->state->src_x = new_state->src_x;
  187. plane->state->src_y = new_state->src_y;
  188. plane->state->src_h = new_state->src_h;
  189. plane->state->src_w = new_state->src_w;
  190. swap(plane->state->fb, new_state->fb);
  191. if (new_state->visible) {
  192. enum lsdc_cursor_size cursor_size;
  193. switch (new_state->crtc_w) {
  194. case 64:
  195. cursor_size = CURSOR_SIZE_64X64;
  196. break;
  197. case 32:
  198. cursor_size = CURSOR_SIZE_32X32;
  199. break;
  200. default:
  201. cursor_size = CURSOR_SIZE_32X32;
  202. break;
  203. }
  204. ops->update_position(cursor, new_state->crtc_x, new_state->crtc_y);
  205. ops->update_cfg(cursor, cursor_size, CURSOR_FORMAT_ARGB8888);
  206. if (!old_fb || old_fb != new_fb)
  207. ops->update_bo_addr(cursor, lsdc_fb_base_addr(new_fb));
  208. }
  209. }
  210. /* ls7a1000 cursor plane helpers */
  211. static int ls7a1000_cursor_plane_atomic_check(struct drm_plane *plane,
  212. struct drm_atomic_state *state)
  213. {
  214. struct drm_plane_state *new_plane_state;
  215. struct drm_crtc_state *new_crtc_state;
  216. struct drm_crtc *crtc;
  217. new_plane_state = drm_atomic_get_new_plane_state(state, plane);
  218. crtc = new_plane_state->crtc;
  219. if (!crtc) {
  220. drm_dbg(plane->dev, "%s is not bind to a crtc\n", plane->name);
  221. return 0;
  222. }
  223. if (new_plane_state->crtc_w != 32 || new_plane_state->crtc_h != 32) {
  224. drm_dbg(plane->dev, "unsupported cursor size: %ux%u\n",
  225. new_plane_state->crtc_w, new_plane_state->crtc_h);
  226. return -EINVAL;
  227. }
  228. new_crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
  229. return drm_atomic_helper_check_plane_state(new_plane_state,
  230. new_crtc_state,
  231. DRM_PLANE_NO_SCALING,
  232. DRM_PLANE_NO_SCALING,
  233. true, true);
  234. }
  235. static void ls7a1000_cursor_plane_atomic_update(struct drm_plane *plane,
  236. struct drm_atomic_state *state)
  237. {
  238. struct lsdc_cursor *cursor = to_lsdc_cursor(plane);
  239. struct drm_plane_state *old_plane_state = drm_atomic_get_old_plane_state(state, plane);
  240. struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(state, plane);
  241. struct drm_framebuffer *new_fb = new_plane_state->fb;
  242. struct drm_framebuffer *old_fb = old_plane_state->fb;
  243. const struct lsdc_cursor_plane_ops *ops = cursor->ops;
  244. u64 addr = lsdc_fb_base_addr(new_fb);
  245. if (!new_plane_state->visible)
  246. return;
  247. ops->update_position(cursor, new_plane_state->crtc_x, new_plane_state->crtc_y);
  248. if (!old_fb || old_fb != new_fb)
  249. ops->update_bo_addr(cursor, addr);
  250. ops->update_cfg(cursor, CURSOR_SIZE_32X32, CURSOR_FORMAT_ARGB8888);
  251. }
  252. static void ls7a1000_cursor_plane_atomic_disable(struct drm_plane *plane,
  253. struct drm_atomic_state *state)
  254. {
  255. struct lsdc_cursor *cursor = to_lsdc_cursor(plane);
  256. const struct lsdc_cursor_plane_ops *ops = cursor->ops;
  257. ops->update_cfg(cursor, CURSOR_SIZE_32X32, CURSOR_FORMAT_DISABLE);
  258. }
  259. static const struct drm_plane_helper_funcs ls7a1000_cursor_plane_helper_funcs = {
  260. .prepare_fb = lsdc_plane_prepare_fb,
  261. .cleanup_fb = lsdc_plane_cleanup_fb,
  262. .atomic_check = ls7a1000_cursor_plane_atomic_check,
  263. .atomic_update = ls7a1000_cursor_plane_atomic_update,
  264. .atomic_disable = ls7a1000_cursor_plane_atomic_disable,
  265. .atomic_async_check = lsdc_cursor_plane_atomic_async_check,
  266. .atomic_async_update = lsdc_cursor_plane_atomic_async_update,
  267. };
  268. /* ls7a2000 cursor plane helpers */
  269. static int ls7a2000_cursor_plane_atomic_check(struct drm_plane *plane,
  270. struct drm_atomic_state *state)
  271. {
  272. struct drm_plane_state *new_plane_state;
  273. struct drm_crtc_state *new_crtc_state;
  274. struct drm_crtc *crtc;
  275. new_plane_state = drm_atomic_get_new_plane_state(state, plane);
  276. crtc = new_plane_state->crtc;
  277. if (!crtc) {
  278. drm_dbg(plane->dev, "%s is not bind to a crtc\n", plane->name);
  279. return 0;
  280. }
  281. if (new_plane_state->crtc_w != new_plane_state->crtc_h) {
  282. drm_dbg(plane->dev, "unsupported cursor size: %ux%u\n",
  283. new_plane_state->crtc_w, new_plane_state->crtc_h);
  284. return -EINVAL;
  285. }
  286. if (new_plane_state->crtc_w != 64 && new_plane_state->crtc_w != 32) {
  287. drm_dbg(plane->dev, "unsupported cursor size: %ux%u\n",
  288. new_plane_state->crtc_w, new_plane_state->crtc_h);
  289. return -EINVAL;
  290. }
  291. new_crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
  292. return drm_atomic_helper_check_plane_state(new_plane_state,
  293. new_crtc_state,
  294. DRM_PLANE_NO_SCALING,
  295. DRM_PLANE_NO_SCALING,
  296. true, true);
  297. }
  298. /* Update the format, size and location of the cursor */
  299. static void ls7a2000_cursor_plane_atomic_update(struct drm_plane *plane,
  300. struct drm_atomic_state *state)
  301. {
  302. struct lsdc_cursor *cursor = to_lsdc_cursor(plane);
  303. struct drm_plane_state *old_plane_state = drm_atomic_get_old_plane_state(state, plane);
  304. struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(state, plane);
  305. struct drm_framebuffer *new_fb = new_plane_state->fb;
  306. struct drm_framebuffer *old_fb = old_plane_state->fb;
  307. const struct lsdc_cursor_plane_ops *ops = cursor->ops;
  308. enum lsdc_cursor_size cursor_size;
  309. if (!new_plane_state->visible)
  310. return;
  311. ops->update_position(cursor, new_plane_state->crtc_x, new_plane_state->crtc_y);
  312. if (!old_fb || new_fb != old_fb) {
  313. u64 addr = lsdc_fb_base_addr(new_fb);
  314. ops->update_bo_addr(cursor, addr);
  315. }
  316. switch (new_plane_state->crtc_w) {
  317. case 64:
  318. cursor_size = CURSOR_SIZE_64X64;
  319. break;
  320. case 32:
  321. cursor_size = CURSOR_SIZE_32X32;
  322. break;
  323. default:
  324. cursor_size = CURSOR_SIZE_64X64;
  325. break;
  326. }
  327. ops->update_cfg(cursor, cursor_size, CURSOR_FORMAT_ARGB8888);
  328. }
  329. static void ls7a2000_cursor_plane_atomic_disable(struct drm_plane *plane,
  330. struct drm_atomic_state *state)
  331. {
  332. struct lsdc_cursor *cursor = to_lsdc_cursor(plane);
  333. const struct lsdc_cursor_plane_ops *hw_ops = cursor->ops;
  334. hw_ops->update_cfg(cursor, CURSOR_SIZE_64X64, CURSOR_FORMAT_DISABLE);
  335. }
  336. static const struct drm_plane_helper_funcs ls7a2000_cursor_plane_helper_funcs = {
  337. .prepare_fb = lsdc_plane_prepare_fb,
  338. .cleanup_fb = lsdc_plane_cleanup_fb,
  339. .atomic_check = ls7a2000_cursor_plane_atomic_check,
  340. .atomic_update = ls7a2000_cursor_plane_atomic_update,
  341. .atomic_disable = ls7a2000_cursor_plane_atomic_disable,
  342. .atomic_async_check = lsdc_cursor_plane_atomic_async_check,
  343. .atomic_async_update = lsdc_cursor_plane_atomic_async_update,
  344. };
  345. static void lsdc_plane_atomic_print_state(struct drm_printer *p,
  346. const struct drm_plane_state *state)
  347. {
  348. struct drm_framebuffer *fb = state->fb;
  349. u64 addr;
  350. if (!fb)
  351. return;
  352. addr = lsdc_fb_base_addr(fb);
  353. drm_printf(p, "\tdma addr=%llx\n", addr);
  354. }
  355. static const struct drm_plane_funcs lsdc_plane_funcs = {
  356. .update_plane = drm_atomic_helper_update_plane,
  357. .disable_plane = drm_atomic_helper_disable_plane,
  358. .destroy = drm_plane_cleanup,
  359. .reset = drm_atomic_helper_plane_reset,
  360. .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
  361. .atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
  362. .atomic_print_state = lsdc_plane_atomic_print_state,
  363. };
  364. /* Primary plane 0 hardware related ops */
  365. static void lsdc_primary0_update_fb_addr(struct lsdc_primary *primary, u64 addr)
  366. {
  367. struct lsdc_device *ldev = primary->ldev;
  368. u32 status;
  369. u32 lo, hi;
  370. /* 40-bit width physical address bus */
  371. lo = addr & 0xFFFFFFFF;
  372. hi = (addr >> 32) & 0xFF;
  373. status = lsdc_rreg32(ldev, LSDC_CRTC0_CFG_REG);
  374. if (status & FB_REG_IN_USING) {
  375. lsdc_wreg32(ldev, LSDC_CRTC0_FB1_ADDR_LO_REG, lo);
  376. lsdc_wreg32(ldev, LSDC_CRTC0_FB1_ADDR_HI_REG, hi);
  377. } else {
  378. lsdc_wreg32(ldev, LSDC_CRTC0_FB0_ADDR_LO_REG, lo);
  379. lsdc_wreg32(ldev, LSDC_CRTC0_FB0_ADDR_HI_REG, hi);
  380. }
  381. }
  382. static void lsdc_primary0_update_fb_stride(struct lsdc_primary *primary, u32 stride)
  383. {
  384. struct lsdc_device *ldev = primary->ldev;
  385. lsdc_wreg32(ldev, LSDC_CRTC0_STRIDE_REG, stride);
  386. }
  387. static void lsdc_primary0_update_fb_format(struct lsdc_primary *primary,
  388. const struct drm_format_info *format)
  389. {
  390. struct lsdc_device *ldev = primary->ldev;
  391. u32 status;
  392. status = lsdc_rreg32(ldev, LSDC_CRTC0_CFG_REG);
  393. /*
  394. * TODO: add RGB565 support, only support XRBG8888 at present
  395. */
  396. status &= ~CFG_PIX_FMT_MASK;
  397. status |= LSDC_PF_XRGB8888;
  398. lsdc_wreg32(ldev, LSDC_CRTC0_CFG_REG, status);
  399. }
  400. /* Primary plane 1 hardware related ops */
  401. static void lsdc_primary1_update_fb_addr(struct lsdc_primary *primary, u64 addr)
  402. {
  403. struct lsdc_device *ldev = primary->ldev;
  404. u32 status;
  405. u32 lo, hi;
  406. /* 40-bit width physical address bus */
  407. lo = addr & 0xFFFFFFFF;
  408. hi = (addr >> 32) & 0xFF;
  409. status = lsdc_rreg32(ldev, LSDC_CRTC1_CFG_REG);
  410. if (status & FB_REG_IN_USING) {
  411. lsdc_wreg32(ldev, LSDC_CRTC1_FB1_ADDR_LO_REG, lo);
  412. lsdc_wreg32(ldev, LSDC_CRTC1_FB1_ADDR_HI_REG, hi);
  413. } else {
  414. lsdc_wreg32(ldev, LSDC_CRTC1_FB0_ADDR_LO_REG, lo);
  415. lsdc_wreg32(ldev, LSDC_CRTC1_FB0_ADDR_HI_REG, hi);
  416. }
  417. }
  418. static void lsdc_primary1_update_fb_stride(struct lsdc_primary *primary, u32 stride)
  419. {
  420. struct lsdc_device *ldev = primary->ldev;
  421. lsdc_wreg32(ldev, LSDC_CRTC1_STRIDE_REG, stride);
  422. }
  423. static void lsdc_primary1_update_fb_format(struct lsdc_primary *primary,
  424. const struct drm_format_info *format)
  425. {
  426. struct lsdc_device *ldev = primary->ldev;
  427. u32 status;
  428. status = lsdc_rreg32(ldev, LSDC_CRTC1_CFG_REG);
  429. /*
  430. * TODO: add RGB565 support, only support XRBG8888 at present
  431. */
  432. status &= ~CFG_PIX_FMT_MASK;
  433. status |= LSDC_PF_XRGB8888;
  434. lsdc_wreg32(ldev, LSDC_CRTC1_CFG_REG, status);
  435. }
  436. static const struct lsdc_primary_plane_ops lsdc_primary_plane_hw_ops[2] = {
  437. {
  438. .update_fb_addr = lsdc_primary0_update_fb_addr,
  439. .update_fb_stride = lsdc_primary0_update_fb_stride,
  440. .update_fb_format = lsdc_primary0_update_fb_format,
  441. },
  442. {
  443. .update_fb_addr = lsdc_primary1_update_fb_addr,
  444. .update_fb_stride = lsdc_primary1_update_fb_stride,
  445. .update_fb_format = lsdc_primary1_update_fb_format,
  446. },
  447. };
  448. /*
  449. * Update location, format, enable and disable state of the cursor,
  450. * For those who have two hardware cursor, let cursor 0 is attach to CRTC-0,
  451. * cursor 1 is attach to CRTC-1. Compositing the primary plane and cursor
  452. * plane is automatically done by hardware, the cursor is alway on the top of
  453. * the primary plane. In other word, z-order is fixed in hardware and cannot
  454. * be changed. For those old DC who has only one hardware cursor, we made it
  455. * shared by the two screen, this works on extend screen mode.
  456. */
  457. /* cursor plane 0 (for pipe 0) related hardware ops */
  458. static void lsdc_cursor0_update_bo_addr(struct lsdc_cursor *cursor, u64 addr)
  459. {
  460. struct lsdc_device *ldev = cursor->ldev;
  461. /* 40-bit width physical address bus */
  462. lsdc_wreg32(ldev, LSDC_CURSOR0_ADDR_HI_REG, (addr >> 32) & 0xFF);
  463. lsdc_wreg32(ldev, LSDC_CURSOR0_ADDR_LO_REG, addr);
  464. }
  465. static void lsdc_cursor0_update_position(struct lsdc_cursor *cursor, int x, int y)
  466. {
  467. struct lsdc_device *ldev = cursor->ldev;
  468. if (x < 0)
  469. x = 0;
  470. if (y < 0)
  471. y = 0;
  472. lsdc_wreg32(ldev, LSDC_CURSOR0_POSITION_REG, (y << 16) | x);
  473. }
  474. static void lsdc_cursor0_update_cfg(struct lsdc_cursor *cursor,
  475. enum lsdc_cursor_size cursor_size,
  476. enum lsdc_cursor_format fmt)
  477. {
  478. struct lsdc_device *ldev = cursor->ldev;
  479. u32 cfg;
  480. cfg = CURSOR_ON_CRTC0 << CURSOR_LOCATION_SHIFT |
  481. cursor_size << CURSOR_SIZE_SHIFT |
  482. fmt << CURSOR_FORMAT_SHIFT;
  483. lsdc_wreg32(ldev, LSDC_CURSOR0_CFG_REG, cfg);
  484. }
  485. /* cursor plane 1 (for pipe 1) related hardware ops */
  486. static void lsdc_cursor1_update_bo_addr(struct lsdc_cursor *cursor, u64 addr)
  487. {
  488. struct lsdc_device *ldev = cursor->ldev;
  489. /* 40-bit width physical address bus */
  490. lsdc_wreg32(ldev, LSDC_CURSOR1_ADDR_HI_REG, (addr >> 32) & 0xFF);
  491. lsdc_wreg32(ldev, LSDC_CURSOR1_ADDR_LO_REG, addr);
  492. }
  493. static void lsdc_cursor1_update_position(struct lsdc_cursor *cursor, int x, int y)
  494. {
  495. struct lsdc_device *ldev = cursor->ldev;
  496. if (x < 0)
  497. x = 0;
  498. if (y < 0)
  499. y = 0;
  500. lsdc_wreg32(ldev, LSDC_CURSOR1_POSITION_REG, (y << 16) | x);
  501. }
  502. static void lsdc_cursor1_update_cfg(struct lsdc_cursor *cursor,
  503. enum lsdc_cursor_size cursor_size,
  504. enum lsdc_cursor_format fmt)
  505. {
  506. struct lsdc_device *ldev = cursor->ldev;
  507. u32 cfg;
  508. cfg = CURSOR_ON_CRTC1 << CURSOR_LOCATION_SHIFT |
  509. cursor_size << CURSOR_SIZE_SHIFT |
  510. fmt << CURSOR_FORMAT_SHIFT;
  511. lsdc_wreg32(ldev, LSDC_CURSOR1_CFG_REG, cfg);
  512. }
  513. /* The hardware cursors become normal since ls7a2000/ls2k2000 */
  514. static const struct lsdc_cursor_plane_ops ls7a2000_cursor_hw_ops[2] = {
  515. {
  516. .update_bo_addr = lsdc_cursor0_update_bo_addr,
  517. .update_cfg = lsdc_cursor0_update_cfg,
  518. .update_position = lsdc_cursor0_update_position,
  519. },
  520. {
  521. .update_bo_addr = lsdc_cursor1_update_bo_addr,
  522. .update_cfg = lsdc_cursor1_update_cfg,
  523. .update_position = lsdc_cursor1_update_position,
  524. },
  525. };
  526. /* Quirks for cursor 1, only for old loongson display controller */
  527. static void lsdc_cursor1_update_bo_addr_quirk(struct lsdc_cursor *cursor, u64 addr)
  528. {
  529. struct lsdc_device *ldev = cursor->ldev;
  530. /* 40-bit width physical address bus */
  531. lsdc_wreg32(ldev, LSDC_CURSOR0_ADDR_HI_REG, (addr >> 32) & 0xFF);
  532. lsdc_wreg32(ldev, LSDC_CURSOR0_ADDR_LO_REG, addr);
  533. }
  534. static void lsdc_cursor1_update_position_quirk(struct lsdc_cursor *cursor, int x, int y)
  535. {
  536. struct lsdc_device *ldev = cursor->ldev;
  537. if (x < 0)
  538. x = 0;
  539. if (y < 0)
  540. y = 0;
  541. lsdc_wreg32(ldev, LSDC_CURSOR0_POSITION_REG, (y << 16) | x);
  542. }
  543. static void lsdc_cursor1_update_cfg_quirk(struct lsdc_cursor *cursor,
  544. enum lsdc_cursor_size cursor_size,
  545. enum lsdc_cursor_format fmt)
  546. {
  547. struct lsdc_device *ldev = cursor->ldev;
  548. u32 cfg;
  549. cfg = CURSOR_ON_CRTC1 << CURSOR_LOCATION_SHIFT |
  550. cursor_size << CURSOR_SIZE_SHIFT |
  551. fmt << CURSOR_FORMAT_SHIFT;
  552. lsdc_wreg32(ldev, LSDC_CURSOR0_CFG_REG, cfg);
  553. }
  554. /*
  555. * The unforgiving LS7A1000/LS2K1000 has only one hardware cursors plane
  556. */
  557. static const struct lsdc_cursor_plane_ops ls7a1000_cursor_hw_ops[2] = {
  558. {
  559. .update_bo_addr = lsdc_cursor0_update_bo_addr,
  560. .update_cfg = lsdc_cursor0_update_cfg,
  561. .update_position = lsdc_cursor0_update_position,
  562. },
  563. {
  564. .update_bo_addr = lsdc_cursor1_update_bo_addr_quirk,
  565. .update_cfg = lsdc_cursor1_update_cfg_quirk,
  566. .update_position = lsdc_cursor1_update_position_quirk,
  567. },
  568. };
  569. int lsdc_primary_plane_init(struct drm_device *ddev,
  570. struct drm_plane *plane,
  571. unsigned int index)
  572. {
  573. struct lsdc_primary *primary = to_lsdc_primary(plane);
  574. int ret;
  575. ret = drm_universal_plane_init(ddev, plane, 1 << index,
  576. &lsdc_plane_funcs,
  577. lsdc_primary_formats,
  578. ARRAY_SIZE(lsdc_primary_formats),
  579. lsdc_fb_format_modifiers,
  580. DRM_PLANE_TYPE_PRIMARY,
  581. "ls-primary-plane-%u", index);
  582. if (ret)
  583. return ret;
  584. drm_plane_helper_add(plane, &lsdc_primary_helper_funcs);
  585. primary->ldev = to_lsdc(ddev);
  586. primary->ops = &lsdc_primary_plane_hw_ops[index];
  587. return 0;
  588. }
  589. int ls7a1000_cursor_plane_init(struct drm_device *ddev,
  590. struct drm_plane *plane,
  591. unsigned int index)
  592. {
  593. struct lsdc_cursor *cursor = to_lsdc_cursor(plane);
  594. int ret;
  595. ret = drm_universal_plane_init(ddev, plane, 1 << index,
  596. &lsdc_plane_funcs,
  597. lsdc_cursor_formats,
  598. ARRAY_SIZE(lsdc_cursor_formats),
  599. lsdc_fb_format_modifiers,
  600. DRM_PLANE_TYPE_CURSOR,
  601. "ls-cursor-plane-%u", index);
  602. if (ret)
  603. return ret;
  604. cursor->ldev = to_lsdc(ddev);
  605. cursor->ops = &ls7a1000_cursor_hw_ops[index];
  606. drm_plane_helper_add(plane, &ls7a1000_cursor_plane_helper_funcs);
  607. return 0;
  608. }
  609. int ls7a2000_cursor_plane_init(struct drm_device *ddev,
  610. struct drm_plane *plane,
  611. unsigned int index)
  612. {
  613. struct lsdc_cursor *cursor = to_lsdc_cursor(plane);
  614. int ret;
  615. ret = drm_universal_plane_init(ddev, plane, 1 << index,
  616. &lsdc_plane_funcs,
  617. lsdc_cursor_formats,
  618. ARRAY_SIZE(lsdc_cursor_formats),
  619. lsdc_fb_format_modifiers,
  620. DRM_PLANE_TYPE_CURSOR,
  621. "ls-cursor-plane-%u", index);
  622. if (ret)
  623. return ret;
  624. cursor->ldev = to_lsdc(ddev);
  625. cursor->ops = &ls7a2000_cursor_hw_ops[index];
  626. drm_plane_helper_add(plane, &ls7a2000_cursor_plane_helper_funcs);
  627. return 0;
  628. }