mgag200_cursor.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /*
  2. * Copyright 2013 Matrox Graphics
  3. *
  4. * This file is subject to the terms and conditions of the GNU General
  5. * Public License version 2. See the file COPYING in the main
  6. * directory of this archive for more details.
  7. *
  8. * Author: Christopher Harvey <charvey@matrox.com>
  9. */
  10. #include <drm/drmP.h>
  11. #include "mgag200_drv.h"
  12. static bool warn_transparent = true;
  13. static bool warn_palette = true;
  14. /*
  15. Hide the cursor off screen. We can't disable the cursor hardware because it
  16. takes too long to re-activate and causes momentary corruption
  17. */
  18. static void mga_hide_cursor(struct mga_device *mdev)
  19. {
  20. WREG8(MGA_CURPOSXL, 0);
  21. WREG8(MGA_CURPOSXH, 0);
  22. if (mdev->cursor.pixels_1->pin_count)
  23. mgag200_bo_unpin(mdev->cursor.pixels_1);
  24. if (mdev->cursor.pixels_2->pin_count)
  25. mgag200_bo_unpin(mdev->cursor.pixels_2);
  26. }
  27. int mga_crtc_cursor_set(struct drm_crtc *crtc,
  28. struct drm_file *file_priv,
  29. uint32_t handle,
  30. uint32_t width,
  31. uint32_t height)
  32. {
  33. struct drm_device *dev = crtc->dev;
  34. struct mga_device *mdev = (struct mga_device *)dev->dev_private;
  35. struct mgag200_bo *pixels_1 = mdev->cursor.pixels_1;
  36. struct mgag200_bo *pixels_2 = mdev->cursor.pixels_2;
  37. struct mgag200_bo *pixels_current = mdev->cursor.pixels_current;
  38. struct mgag200_bo *pixels_prev = mdev->cursor.pixels_prev;
  39. struct drm_gem_object *obj;
  40. struct mgag200_bo *bo = NULL;
  41. int ret = 0;
  42. unsigned int i, row, col;
  43. uint32_t colour_set[16];
  44. uint32_t *next_space = &colour_set[0];
  45. uint32_t *palette_iter;
  46. uint32_t this_colour;
  47. bool found = false;
  48. int colour_count = 0;
  49. u64 gpu_addr;
  50. u8 reg_index;
  51. u8 this_row[48];
  52. if (!pixels_1 || !pixels_2) {
  53. WREG8(MGA_CURPOSXL, 0);
  54. WREG8(MGA_CURPOSXH, 0);
  55. return -ENOTSUPP; /* Didn't allocate space for cursors */
  56. }
  57. if ((width != 64 || height != 64) && handle) {
  58. WREG8(MGA_CURPOSXL, 0);
  59. WREG8(MGA_CURPOSXH, 0);
  60. return -EINVAL;
  61. }
  62. BUG_ON(pixels_1 != pixels_current && pixels_1 != pixels_prev);
  63. BUG_ON(pixels_2 != pixels_current && pixels_2 != pixels_prev);
  64. BUG_ON(pixels_current == pixels_prev);
  65. if (!handle || !file_priv) {
  66. mga_hide_cursor(mdev);
  67. return 0;
  68. }
  69. obj = drm_gem_object_lookup(dev, file_priv, handle);
  70. if (!obj)
  71. return -ENOENT;
  72. ret = mgag200_bo_reserve(pixels_1, true);
  73. if (ret) {
  74. WREG8(MGA_CURPOSXL, 0);
  75. WREG8(MGA_CURPOSXH, 0);
  76. goto out_unref;
  77. }
  78. ret = mgag200_bo_reserve(pixels_2, true);
  79. if (ret) {
  80. WREG8(MGA_CURPOSXL, 0);
  81. WREG8(MGA_CURPOSXH, 0);
  82. mgag200_bo_unreserve(pixels_1);
  83. goto out_unreserve1;
  84. }
  85. /* Move cursor buffers into VRAM if they aren't already */
  86. if (!pixels_1->pin_count) {
  87. ret = mgag200_bo_pin(pixels_1, TTM_PL_FLAG_VRAM,
  88. &mdev->cursor.pixels_1_gpu_addr);
  89. if (ret)
  90. goto out1;
  91. }
  92. if (!pixels_2->pin_count) {
  93. ret = mgag200_bo_pin(pixels_2, TTM_PL_FLAG_VRAM,
  94. &mdev->cursor.pixels_2_gpu_addr);
  95. if (ret) {
  96. mgag200_bo_unpin(pixels_1);
  97. goto out1;
  98. }
  99. }
  100. bo = gem_to_mga_bo(obj);
  101. ret = mgag200_bo_reserve(bo, true);
  102. if (ret) {
  103. dev_err(&dev->pdev->dev, "failed to reserve user bo\n");
  104. goto out1;
  105. }
  106. if (!bo->kmap.virtual) {
  107. ret = ttm_bo_kmap(&bo->bo, 0, bo->bo.num_pages, &bo->kmap);
  108. if (ret) {
  109. dev_err(&dev->pdev->dev, "failed to kmap user buffer updates\n");
  110. goto out2;
  111. }
  112. }
  113. memset(&colour_set[0], 0, sizeof(uint32_t)*16);
  114. /* width*height*4 = 16384 */
  115. for (i = 0; i < 16384; i += 4) {
  116. this_colour = ioread32(bo->kmap.virtual + i);
  117. /* No transparency */
  118. if (this_colour>>24 != 0xff &&
  119. this_colour>>24 != 0x0) {
  120. if (warn_transparent) {
  121. dev_info(&dev->pdev->dev, "Video card doesn't support cursors with partial transparency.\n");
  122. dev_info(&dev->pdev->dev, "Not enabling hardware cursor.\n");
  123. warn_transparent = false; /* Only tell the user once. */
  124. }
  125. ret = -EINVAL;
  126. goto out3;
  127. }
  128. /* Don't need to store transparent pixels as colours */
  129. if (this_colour>>24 == 0x0)
  130. continue;
  131. found = false;
  132. for (palette_iter = &colour_set[0]; palette_iter != next_space; palette_iter++) {
  133. if (*palette_iter == this_colour) {
  134. found = true;
  135. break;
  136. }
  137. }
  138. if (found)
  139. continue;
  140. /* We only support 4bit paletted cursors */
  141. if (colour_count >= 16) {
  142. if (warn_palette) {
  143. dev_info(&dev->pdev->dev, "Video card only supports cursors with up to 16 colours.\n");
  144. dev_info(&dev->pdev->dev, "Not enabling hardware cursor.\n");
  145. warn_palette = false; /* Only tell the user once. */
  146. }
  147. ret = -EINVAL;
  148. goto out3;
  149. }
  150. *next_space = this_colour;
  151. next_space++;
  152. colour_count++;
  153. }
  154. /* Program colours from cursor icon into palette */
  155. for (i = 0; i < colour_count; i++) {
  156. if (i <= 2)
  157. reg_index = 0x8 + i*0x4;
  158. else
  159. reg_index = 0x60 + i*0x3;
  160. WREG_DAC(reg_index, colour_set[i] & 0xff);
  161. WREG_DAC(reg_index+1, colour_set[i]>>8 & 0xff);
  162. WREG_DAC(reg_index+2, colour_set[i]>>16 & 0xff);
  163. BUG_ON((colour_set[i]>>24 & 0xff) != 0xff);
  164. }
  165. /* Map up-coming buffer to write colour indices */
  166. if (!pixels_prev->kmap.virtual) {
  167. ret = ttm_bo_kmap(&pixels_prev->bo, 0,
  168. pixels_prev->bo.num_pages,
  169. &pixels_prev->kmap);
  170. if (ret) {
  171. dev_err(&dev->pdev->dev, "failed to kmap cursor updates\n");
  172. goto out3;
  173. }
  174. }
  175. /* now write colour indices into hardware cursor buffer */
  176. for (row = 0; row < 64; row++) {
  177. memset(&this_row[0], 0, 48);
  178. for (col = 0; col < 64; col++) {
  179. this_colour = ioread32(bo->kmap.virtual + 4*(col + 64*row));
  180. /* write transparent pixels */
  181. if (this_colour>>24 == 0x0) {
  182. this_row[47 - col/8] |= 0x80>>(col%8);
  183. continue;
  184. }
  185. /* write colour index here */
  186. for (i = 0; i < colour_count; i++) {
  187. if (colour_set[i] == this_colour) {
  188. if (col % 2)
  189. this_row[col/2] |= i<<4;
  190. else
  191. this_row[col/2] |= i;
  192. break;
  193. }
  194. }
  195. }
  196. memcpy_toio(pixels_prev->kmap.virtual + row*48, &this_row[0], 48);
  197. }
  198. /* Program gpu address of cursor buffer */
  199. if (pixels_prev == pixels_1)
  200. gpu_addr = mdev->cursor.pixels_1_gpu_addr;
  201. else
  202. gpu_addr = mdev->cursor.pixels_2_gpu_addr;
  203. WREG_DAC(MGA1064_CURSOR_BASE_ADR_LOW, (u8)((gpu_addr>>10) & 0xff));
  204. WREG_DAC(MGA1064_CURSOR_BASE_ADR_HI, (u8)((gpu_addr>>18) & 0x3f));
  205. /* Adjust cursor control register to turn on the cursor */
  206. WREG_DAC(MGA1064_CURSOR_CTL, 4); /* 16-colour palletized cursor mode */
  207. /* Now swap internal buffer pointers */
  208. if (mdev->cursor.pixels_1 == mdev->cursor.pixels_prev) {
  209. mdev->cursor.pixels_prev = mdev->cursor.pixels_2;
  210. mdev->cursor.pixels_current = mdev->cursor.pixels_1;
  211. } else if (mdev->cursor.pixels_1 == mdev->cursor.pixels_current) {
  212. mdev->cursor.pixels_prev = mdev->cursor.pixels_1;
  213. mdev->cursor.pixels_current = mdev->cursor.pixels_2;
  214. } else {
  215. BUG();
  216. }
  217. ret = 0;
  218. ttm_bo_kunmap(&pixels_prev->kmap);
  219. out3:
  220. ttm_bo_kunmap(&bo->kmap);
  221. out2:
  222. mgag200_bo_unreserve(bo);
  223. out1:
  224. if (ret)
  225. mga_hide_cursor(mdev);
  226. mgag200_bo_unreserve(pixels_1);
  227. out_unreserve1:
  228. mgag200_bo_unreserve(pixels_2);
  229. out_unref:
  230. drm_gem_object_unreference_unlocked(obj);
  231. return ret;
  232. }
  233. int mga_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
  234. {
  235. struct mga_device *mdev = (struct mga_device *)crtc->dev->dev_private;
  236. /* Our origin is at (64,64) */
  237. x += 64;
  238. y += 64;
  239. BUG_ON(x <= 0);
  240. BUG_ON(y <= 0);
  241. BUG_ON(x & ~0xffff);
  242. BUG_ON(y & ~0xffff);
  243. WREG8(MGA_CURPOSXL, x & 0xff);
  244. WREG8(MGA_CURPOSXH, (x>>8) & 0xff);
  245. WREG8(MGA_CURPOSYL, y & 0xff);
  246. WREG8(MGA_CURPOSYH, (y>>8) & 0xff);
  247. return 0;
  248. }