mgag200_cursor.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. obj = drm_gem_object_lookup(dev, file_priv, handle);
  66. if (!obj)
  67. return -ENOENT;
  68. ret = mgag200_bo_reserve(pixels_1, true);
  69. if (ret) {
  70. WREG8(MGA_CURPOSXL, 0);
  71. WREG8(MGA_CURPOSXH, 0);
  72. goto out_unref;
  73. }
  74. ret = mgag200_bo_reserve(pixels_2, true);
  75. if (ret) {
  76. WREG8(MGA_CURPOSXL, 0);
  77. WREG8(MGA_CURPOSXH, 0);
  78. mgag200_bo_unreserve(pixels_1);
  79. goto out_unreserve1;
  80. }
  81. if (!handle) {
  82. mga_hide_cursor(mdev);
  83. ret = 0;
  84. goto out1;
  85. }
  86. /* Move cursor buffers into VRAM if they aren't already */
  87. if (!pixels_1->pin_count) {
  88. ret = mgag200_bo_pin(pixels_1, TTM_PL_FLAG_VRAM,
  89. &mdev->cursor.pixels_1_gpu_addr);
  90. if (ret)
  91. goto out1;
  92. }
  93. if (!pixels_2->pin_count) {
  94. ret = mgag200_bo_pin(pixels_2, TTM_PL_FLAG_VRAM,
  95. &mdev->cursor.pixels_2_gpu_addr);
  96. if (ret) {
  97. mgag200_bo_unpin(pixels_1);
  98. goto out1;
  99. }
  100. }
  101. bo = gem_to_mga_bo(obj);
  102. ret = mgag200_bo_reserve(bo, true);
  103. if (ret) {
  104. dev_err(&dev->pdev->dev, "failed to reserve user bo\n");
  105. goto out1;
  106. }
  107. if (!bo->kmap.virtual) {
  108. ret = ttm_bo_kmap(&bo->bo, 0, bo->bo.num_pages, &bo->kmap);
  109. if (ret) {
  110. dev_err(&dev->pdev->dev, "failed to kmap user buffer updates\n");
  111. goto out2;
  112. }
  113. }
  114. memset(&colour_set[0], 0, sizeof(uint32_t)*16);
  115. /* width*height*4 = 16384 */
  116. for (i = 0; i < 16384; i += 4) {
  117. this_colour = ioread32(bo->kmap.virtual + i);
  118. /* No transparency */
  119. if (this_colour>>24 != 0xff &&
  120. this_colour>>24 != 0x0) {
  121. if (warn_transparent) {
  122. dev_info(&dev->pdev->dev, "Video card doesn't support cursors with partial transparency.\n");
  123. dev_info(&dev->pdev->dev, "Not enabling hardware cursor.\n");
  124. warn_transparent = false; /* Only tell the user once. */
  125. }
  126. ret = -EINVAL;
  127. goto out3;
  128. }
  129. /* Don't need to store transparent pixels as colours */
  130. if (this_colour>>24 == 0x0)
  131. continue;
  132. found = false;
  133. for (palette_iter = &colour_set[0]; palette_iter != next_space; palette_iter++) {
  134. if (*palette_iter == this_colour) {
  135. found = true;
  136. break;
  137. }
  138. }
  139. if (found)
  140. continue;
  141. /* We only support 4bit paletted cursors */
  142. if (colour_count >= 16) {
  143. if (warn_palette) {
  144. dev_info(&dev->pdev->dev, "Video card only supports cursors with up to 16 colours.\n");
  145. dev_info(&dev->pdev->dev, "Not enabling hardware cursor.\n");
  146. warn_palette = false; /* Only tell the user once. */
  147. }
  148. ret = -EINVAL;
  149. goto out3;
  150. }
  151. *next_space = this_colour;
  152. next_space++;
  153. colour_count++;
  154. }
  155. /* Program colours from cursor icon into palette */
  156. for (i = 0; i < colour_count; i++) {
  157. if (i <= 2)
  158. reg_index = 0x8 + i*0x4;
  159. else
  160. reg_index = 0x60 + i*0x3;
  161. WREG_DAC(reg_index, colour_set[i] & 0xff);
  162. WREG_DAC(reg_index+1, colour_set[i]>>8 & 0xff);
  163. WREG_DAC(reg_index+2, colour_set[i]>>16 & 0xff);
  164. BUG_ON((colour_set[i]>>24 & 0xff) != 0xff);
  165. }
  166. /* Map up-coming buffer to write colour indices */
  167. if (!pixels_prev->kmap.virtual) {
  168. ret = ttm_bo_kmap(&pixels_prev->bo, 0,
  169. pixels_prev->bo.num_pages,
  170. &pixels_prev->kmap);
  171. if (ret) {
  172. dev_err(&dev->pdev->dev, "failed to kmap cursor updates\n");
  173. goto out3;
  174. }
  175. }
  176. /* now write colour indices into hardware cursor buffer */
  177. for (row = 0; row < 64; row++) {
  178. memset(&this_row[0], 0, 48);
  179. for (col = 0; col < 64; col++) {
  180. this_colour = ioread32(bo->kmap.virtual + 4*(col + 64*row));
  181. /* write transparent pixels */
  182. if (this_colour>>24 == 0x0) {
  183. this_row[47 - col/8] |= 0x80>>(col%8);
  184. continue;
  185. }
  186. /* write colour index here */
  187. for (i = 0; i < colour_count; i++) {
  188. if (colour_set[i] == this_colour) {
  189. if (col % 2)
  190. this_row[col/2] |= i<<4;
  191. else
  192. this_row[col/2] |= i;
  193. break;
  194. }
  195. }
  196. }
  197. memcpy_toio(pixels_prev->kmap.virtual + row*48, &this_row[0], 48);
  198. }
  199. /* Program gpu address of cursor buffer */
  200. if (pixels_prev == pixels_1)
  201. gpu_addr = mdev->cursor.pixels_1_gpu_addr;
  202. else
  203. gpu_addr = mdev->cursor.pixels_2_gpu_addr;
  204. WREG_DAC(MGA1064_CURSOR_BASE_ADR_LOW, (u8)((gpu_addr>>10) & 0xff));
  205. WREG_DAC(MGA1064_CURSOR_BASE_ADR_HI, (u8)((gpu_addr>>18) & 0x3f));
  206. /* Adjust cursor control register to turn on the cursor */
  207. WREG_DAC(MGA1064_CURSOR_CTL, 4); /* 16-colour palletized cursor mode */
  208. /* Now swap internal buffer pointers */
  209. if (mdev->cursor.pixels_1 == mdev->cursor.pixels_prev) {
  210. mdev->cursor.pixels_prev = mdev->cursor.pixels_2;
  211. mdev->cursor.pixels_current = mdev->cursor.pixels_1;
  212. } else if (mdev->cursor.pixels_1 == mdev->cursor.pixels_current) {
  213. mdev->cursor.pixels_prev = mdev->cursor.pixels_1;
  214. mdev->cursor.pixels_current = mdev->cursor.pixels_2;
  215. } else {
  216. BUG();
  217. }
  218. ret = 0;
  219. ttm_bo_kunmap(&pixels_prev->kmap);
  220. out3:
  221. ttm_bo_kunmap(&bo->kmap);
  222. out2:
  223. mgag200_bo_unreserve(bo);
  224. out1:
  225. if (ret)
  226. mga_hide_cursor(mdev);
  227. mgag200_bo_unreserve(pixels_1);
  228. out_unreserve1:
  229. mgag200_bo_unreserve(pixels_2);
  230. out_unref:
  231. drm_gem_object_unreference_unlocked(obj);
  232. return ret;
  233. }
  234. int mga_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
  235. {
  236. struct mga_device *mdev = (struct mga_device *)crtc->dev->dev_private;
  237. /* Our origin is at (64,64) */
  238. x += 64;
  239. y += 64;
  240. BUG_ON(x <= 0);
  241. BUG_ON(y <= 0);
  242. BUG_ON(x & ~0xffff);
  243. BUG_ON(y & ~0xffff);
  244. WREG8(MGA_CURPOSXL, x & 0xff);
  245. WREG8(MGA_CURPOSXH, (x>>8) & 0xff);
  246. WREG8(MGA_CURPOSYL, y & 0xff);
  247. WREG8(MGA_CURPOSYH, (y>>8) & 0xff);
  248. return 0;
  249. }