mgag200_cursor.c 7.3 KB

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