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