accel_2d.c 9.4 KB

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