accel_2d.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /**************************************************************************
  3. * Copyright (c) 2007-2011, Intel Corporation.
  4. * All Rights Reserved.
  5. *
  6. * Intel funded Tungsten Graphics (http://www.tungstengraphics.com) to
  7. * develop this driver.
  8. *
  9. **************************************************************************/
  10. #include <linux/console.h>
  11. #include <linux/delay.h>
  12. #include <linux/errno.h>
  13. #include <linux/init.h>
  14. #include <linux/kernel.h>
  15. #include <linux/mm.h>
  16. #include <linux/module.h>
  17. #include <linux/slab.h>
  18. #include <linux/string.h>
  19. #include <linux/tty.h>
  20. #include <drm/drm.h>
  21. #include <drm/drm_crtc.h>
  22. #include <drm/drm_fb_helper.h>
  23. #include <drm/drm_fourcc.h>
  24. #include "psb_drv.h"
  25. #include "psb_reg.h"
  26. /**
  27. * psb_spank - reset the 2D engine
  28. * @dev_priv: our PSB DRM device
  29. *
  30. * Soft reset the graphics engine and then reload the necessary registers.
  31. * We use this at initialisation time but it will become relevant for
  32. * accelerated X later
  33. */
  34. void psb_spank(struct drm_psb_private *dev_priv)
  35. {
  36. PSB_WSGX32(_PSB_CS_RESET_BIF_RESET | _PSB_CS_RESET_DPM_RESET |
  37. _PSB_CS_RESET_TA_RESET | _PSB_CS_RESET_USE_RESET |
  38. _PSB_CS_RESET_ISP_RESET | _PSB_CS_RESET_TSP_RESET |
  39. _PSB_CS_RESET_TWOD_RESET, PSB_CR_SOFT_RESET);
  40. PSB_RSGX32(PSB_CR_SOFT_RESET);
  41. msleep(1);
  42. PSB_WSGX32(0, PSB_CR_SOFT_RESET);
  43. wmb();
  44. PSB_WSGX32(PSB_RSGX32(PSB_CR_BIF_CTRL) | _PSB_CB_CTRL_CLEAR_FAULT,
  45. PSB_CR_BIF_CTRL);
  46. wmb();
  47. (void) PSB_RSGX32(PSB_CR_BIF_CTRL);
  48. msleep(1);
  49. PSB_WSGX32(PSB_RSGX32(PSB_CR_BIF_CTRL) & ~_PSB_CB_CTRL_CLEAR_FAULT,
  50. PSB_CR_BIF_CTRL);
  51. (void) PSB_RSGX32(PSB_CR_BIF_CTRL);
  52. PSB_WSGX32(dev_priv->gtt.gatt_start, PSB_CR_BIF_TWOD_REQ_BASE);
  53. }
  54. /**
  55. * psb2_2d_wait_available - wait for FIFO room
  56. * @dev_priv: our DRM device
  57. * @size: size (in dwords) of the command we want to issue
  58. *
  59. * Wait until there is room to load the FIFO with our data. If the
  60. * device is not responding then reset it
  61. */
  62. static int psb_2d_wait_available(struct drm_psb_private *dev_priv,
  63. unsigned size)
  64. {
  65. uint32_t avail = PSB_RSGX32(PSB_CR_2D_SOCIF);
  66. unsigned long t = jiffies + HZ;
  67. while (avail < size) {
  68. avail = PSB_RSGX32(PSB_CR_2D_SOCIF);
  69. if (time_after(jiffies, t)) {
  70. psb_spank(dev_priv);
  71. return -EIO;
  72. }
  73. }
  74. return 0;
  75. }
  76. /**
  77. * psb_2d_submit - submit a 2D command
  78. * @dev_priv: our DRM device
  79. * @cmdbuf: command to issue
  80. * @size: length (in dwords)
  81. *
  82. * Issue one or more 2D commands to the accelerator. This needs to be
  83. * serialized later when we add the GEM interfaces for acceleration
  84. */
  85. static int psbfb_2d_submit(struct drm_psb_private *dev_priv, uint32_t *cmdbuf,
  86. unsigned size)
  87. {
  88. int ret = 0;
  89. int i;
  90. unsigned submit_size;
  91. unsigned long flags;
  92. spin_lock_irqsave(&dev_priv->lock_2d, flags);
  93. while (size > 0) {
  94. submit_size = (size < 0x60) ? size : 0x60;
  95. size -= submit_size;
  96. ret = psb_2d_wait_available(dev_priv, submit_size);
  97. if (ret)
  98. break;
  99. submit_size <<= 2;
  100. for (i = 0; i < submit_size; i += 4)
  101. PSB_WSGX32(*cmdbuf++, PSB_SGX_2D_SLAVE_PORT + i);
  102. (void)PSB_RSGX32(PSB_SGX_2D_SLAVE_PORT + i - 4);
  103. }
  104. spin_unlock_irqrestore(&dev_priv->lock_2d, flags);
  105. return ret;
  106. }
  107. /**
  108. * psb_accel_2d_copy_direction - compute blit order
  109. * @xdir: X direction of move
  110. * @ydir: Y direction of move
  111. *
  112. * Compute the correct order setings to ensure that an overlapping blit
  113. * correctly copies all the pixels.
  114. */
  115. static u32 psb_accel_2d_copy_direction(int xdir, int ydir)
  116. {
  117. if (xdir < 0)
  118. return (ydir < 0) ? PSB_2D_COPYORDER_BR2TL :
  119. PSB_2D_COPYORDER_TR2BL;
  120. else
  121. return (ydir < 0) ? PSB_2D_COPYORDER_BL2TR :
  122. PSB_2D_COPYORDER_TL2BR;
  123. }
  124. /**
  125. * psb_accel_2d_copy - accelerated 2D copy
  126. * @dev_priv: our DRM device
  127. * @src_offset in bytes
  128. * @src_stride in bytes
  129. * @src_format psb 2D format defines
  130. * @dst_offset in bytes
  131. * @dst_stride in bytes
  132. * @dst_format psb 2D format defines
  133. * @src_x offset in pixels
  134. * @src_y offset in pixels
  135. * @dst_x offset in pixels
  136. * @dst_y offset in pixels
  137. * @size_x of the copied area
  138. * @size_y of the copied area
  139. *
  140. * Format and issue a 2D accelerated copy command.
  141. */
  142. static int psb_accel_2d_copy(struct drm_psb_private *dev_priv,
  143. uint32_t src_offset, uint32_t src_stride,
  144. uint32_t src_format, uint32_t dst_offset,
  145. uint32_t dst_stride, uint32_t dst_format,
  146. uint16_t src_x, uint16_t src_y,
  147. uint16_t dst_x, uint16_t dst_y,
  148. uint16_t size_x, uint16_t size_y)
  149. {
  150. uint32_t blit_cmd;
  151. uint32_t buffer[10];
  152. uint32_t *buf;
  153. uint32_t direction;
  154. buf = buffer;
  155. direction =
  156. psb_accel_2d_copy_direction(src_x - dst_x, src_y - dst_y);
  157. if (direction == PSB_2D_COPYORDER_BR2TL ||
  158. direction == PSB_2D_COPYORDER_TR2BL) {
  159. src_x += size_x - 1;
  160. dst_x += size_x - 1;
  161. }
  162. if (direction == PSB_2D_COPYORDER_BR2TL ||
  163. direction == PSB_2D_COPYORDER_BL2TR) {
  164. src_y += size_y - 1;
  165. dst_y += size_y - 1;
  166. }
  167. blit_cmd =
  168. PSB_2D_BLIT_BH |
  169. PSB_2D_ROT_NONE |
  170. PSB_2D_DSTCK_DISABLE |
  171. PSB_2D_SRCCK_DISABLE |
  172. PSB_2D_USE_PAT | PSB_2D_ROP3_SRCCOPY | direction;
  173. *buf++ = PSB_2D_FENCE_BH;
  174. *buf++ =
  175. PSB_2D_DST_SURF_BH | dst_format | (dst_stride <<
  176. PSB_2D_DST_STRIDE_SHIFT);
  177. *buf++ = dst_offset;
  178. *buf++ =
  179. PSB_2D_SRC_SURF_BH | src_format | (src_stride <<
  180. PSB_2D_SRC_STRIDE_SHIFT);
  181. *buf++ = src_offset;
  182. *buf++ =
  183. PSB_2D_SRC_OFF_BH | (src_x << PSB_2D_SRCOFF_XSTART_SHIFT) |
  184. (src_y << PSB_2D_SRCOFF_YSTART_SHIFT);
  185. *buf++ = blit_cmd;
  186. *buf++ =
  187. (dst_x << PSB_2D_DST_XSTART_SHIFT) | (dst_y <<
  188. PSB_2D_DST_YSTART_SHIFT);
  189. *buf++ =
  190. (size_x << PSB_2D_DST_XSIZE_SHIFT) | (size_y <<
  191. PSB_2D_DST_YSIZE_SHIFT);
  192. *buf++ = PSB_2D_FLUSH_BH;
  193. return psbfb_2d_submit(dev_priv, buffer, buf - buffer);
  194. }
  195. /**
  196. * psbfb_copyarea_accel - copyarea acceleration for /dev/fb
  197. * @info: our framebuffer
  198. * @a: copyarea parameters from the framebuffer core
  199. *
  200. * Perform a 2D copy via the accelerator
  201. */
  202. static void psbfb_copyarea_accel(struct fb_info *info,
  203. const struct fb_copyarea *a)
  204. {
  205. struct drm_fb_helper *fb_helper = info->par;
  206. struct drm_framebuffer *fb = fb_helper->fb;
  207. struct drm_device *dev;
  208. struct drm_psb_private *dev_priv;
  209. uint32_t offset;
  210. uint32_t stride;
  211. uint32_t src_format;
  212. uint32_t dst_format;
  213. if (!fb)
  214. return;
  215. dev = fb->dev;
  216. dev_priv = dev->dev_private;
  217. offset = to_gtt_range(fb->obj[0])->offset;
  218. stride = fb->pitches[0];
  219. switch (fb->format->depth) {
  220. case 8:
  221. src_format = PSB_2D_SRC_332RGB;
  222. dst_format = PSB_2D_DST_332RGB;
  223. break;
  224. case 15:
  225. src_format = PSB_2D_SRC_555RGB;
  226. dst_format = PSB_2D_DST_555RGB;
  227. break;
  228. case 16:
  229. src_format = PSB_2D_SRC_565RGB;
  230. dst_format = PSB_2D_DST_565RGB;
  231. break;
  232. case 24:
  233. case 32:
  234. /* this is wrong but since we don't do blending its okay */
  235. src_format = PSB_2D_SRC_8888ARGB;
  236. dst_format = PSB_2D_DST_8888ARGB;
  237. break;
  238. default:
  239. /* software fallback */
  240. drm_fb_helper_cfb_copyarea(info, a);
  241. return;
  242. }
  243. if (!gma_power_begin(dev, false)) {
  244. drm_fb_helper_cfb_copyarea(info, a);
  245. return;
  246. }
  247. psb_accel_2d_copy(dev_priv,
  248. offset, stride, src_format,
  249. offset, stride, dst_format,
  250. a->sx, a->sy, a->dx, a->dy, a->width, a->height);
  251. gma_power_end(dev);
  252. }
  253. /**
  254. * psbfb_copyarea - 2D copy interface
  255. * @info: our framebuffer
  256. * @region: region to copy
  257. *
  258. * Copy an area of the framebuffer console either by the accelerator
  259. * or directly using the cfb helpers according to the request
  260. */
  261. void psbfb_copyarea(struct fb_info *info,
  262. const struct fb_copyarea *region)
  263. {
  264. if (unlikely(info->state != FBINFO_STATE_RUNNING))
  265. return;
  266. /* Avoid the 8 pixel erratum */
  267. if (region->width == 8 || region->height == 8 ||
  268. (info->flags & FBINFO_HWACCEL_DISABLED))
  269. return drm_fb_helper_cfb_copyarea(info, region);
  270. psbfb_copyarea_accel(info, region);
  271. }
  272. /**
  273. * psbfb_sync - synchronize 2D
  274. * @info: our framebuffer
  275. *
  276. * Wait for the 2D engine to quiesce so that we can do CPU
  277. * access to the framebuffer again
  278. */
  279. int psbfb_sync(struct fb_info *info)
  280. {
  281. struct drm_fb_helper *fb_helper = info->par;
  282. struct drm_framebuffer *fb = fb_helper->fb;
  283. struct drm_device *dev = fb->dev;
  284. struct drm_psb_private *dev_priv = dev->dev_private;
  285. unsigned long _end = jiffies + HZ;
  286. int busy = 0;
  287. unsigned long flags;
  288. spin_lock_irqsave(&dev_priv->lock_2d, flags);
  289. /*
  290. * First idle the 2D engine.
  291. */
  292. if ((PSB_RSGX32(PSB_CR_2D_SOCIF) == _PSB_C2_SOCIF_EMPTY) &&
  293. ((PSB_RSGX32(PSB_CR_2D_BLIT_STATUS) & _PSB_C2B_STATUS_BUSY) == 0))
  294. goto out;
  295. do {
  296. busy = (PSB_RSGX32(PSB_CR_2D_SOCIF) != _PSB_C2_SOCIF_EMPTY);
  297. cpu_relax();
  298. } while (busy && !time_after_eq(jiffies, _end));
  299. if (busy)
  300. busy = (PSB_RSGX32(PSB_CR_2D_SOCIF) != _PSB_C2_SOCIF_EMPTY);
  301. if (busy)
  302. goto out;
  303. do {
  304. busy = ((PSB_RSGX32(PSB_CR_2D_BLIT_STATUS) &
  305. _PSB_C2B_STATUS_BUSY) != 0);
  306. cpu_relax();
  307. } while (busy && !time_after_eq(jiffies, _end));
  308. if (busy)
  309. busy = ((PSB_RSGX32(PSB_CR_2D_BLIT_STATUS) &
  310. _PSB_C2B_STATUS_BUSY) != 0);
  311. out:
  312. spin_unlock_irqrestore(&dev_priv->lock_2d, flags);
  313. return (busy) ? -EBUSY : 0;
  314. }