vrfb.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /*
  2. * VRFB Rotation Engine
  3. *
  4. * Copyright (C) 2009 Nokia Corporation
  5. * Author: Tomi Valkeinen <tomi.valkeinen@nokia.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19. */
  20. /*#define DEBUG*/
  21. #include <linux/kernel.h>
  22. #include <linux/module.h>
  23. #include <linux/ioport.h>
  24. #include <linux/io.h>
  25. #include <linux/bitops.h>
  26. #include <linux/mutex.h>
  27. #include <plat/vrfb.h>
  28. #include <plat/sdrc.h>
  29. #ifdef DEBUG
  30. #define DBG(format, ...) pr_debug("VRFB: " format, ## __VA_ARGS__)
  31. #else
  32. #define DBG(format, ...)
  33. #endif
  34. #define SMS_ROT_VIRT_BASE(context, rot) \
  35. (((context >= 4) ? 0xD0000000 : 0x70000000) \
  36. + (0x4000000 * (context)) \
  37. + (0x1000000 * (rot)))
  38. #define OMAP_VRFB_SIZE (2048 * 2048 * 4)
  39. #define VRFB_PAGE_WIDTH_EXP 5 /* Assuming SDRAM pagesize= 1024 */
  40. #define VRFB_PAGE_HEIGHT_EXP 5 /* 1024 = 2^5 * 2^5 */
  41. #define VRFB_PAGE_WIDTH (1 << VRFB_PAGE_WIDTH_EXP)
  42. #define VRFB_PAGE_HEIGHT (1 << VRFB_PAGE_HEIGHT_EXP)
  43. #define SMS_IMAGEHEIGHT_OFFSET 16
  44. #define SMS_IMAGEWIDTH_OFFSET 0
  45. #define SMS_PH_OFFSET 8
  46. #define SMS_PW_OFFSET 4
  47. #define SMS_PS_OFFSET 0
  48. #define VRFB_NUM_CTXS 12
  49. /* bitmap of reserved contexts */
  50. static unsigned long ctx_map;
  51. static DEFINE_MUTEX(ctx_lock);
  52. /*
  53. * Access to this happens from client drivers or the PM core after wake-up.
  54. * For the first case we require locking at the driver level, for the second
  55. * we don't need locking, since no drivers will run until after the wake-up
  56. * has finished.
  57. */
  58. static struct {
  59. u32 physical_ba;
  60. u32 control;
  61. u32 size;
  62. } vrfb_hw_context[VRFB_NUM_CTXS];
  63. static inline void restore_hw_context(int ctx)
  64. {
  65. omap2_sms_write_rot_control(vrfb_hw_context[ctx].control, ctx);
  66. omap2_sms_write_rot_size(vrfb_hw_context[ctx].size, ctx);
  67. omap2_sms_write_rot_physical_ba(vrfb_hw_context[ctx].physical_ba, ctx);
  68. }
  69. static u32 get_image_width_roundup(u16 width, u8 bytespp)
  70. {
  71. unsigned long stride = width * bytespp;
  72. unsigned long ceil_pages_per_stride = (stride / VRFB_PAGE_WIDTH) +
  73. (stride % VRFB_PAGE_WIDTH != 0);
  74. return ceil_pages_per_stride * VRFB_PAGE_WIDTH / bytespp;
  75. }
  76. /*
  77. * This the extra space needed in the VRFB physical area for VRFB to safely wrap
  78. * any memory accesses to the invisible part of the virtual view to the physical
  79. * area.
  80. */
  81. static inline u32 get_extra_physical_size(u16 image_width_roundup, u8 bytespp)
  82. {
  83. return (OMAP_VRFB_LINE_LEN - image_width_roundup) * VRFB_PAGE_HEIGHT *
  84. bytespp;
  85. }
  86. void omap_vrfb_restore_context(void)
  87. {
  88. int i;
  89. unsigned long map = ctx_map;
  90. for (i = ffs(map); i; i = ffs(map)) {
  91. /* i=1..32 */
  92. i--;
  93. map &= ~(1 << i);
  94. restore_hw_context(i);
  95. }
  96. }
  97. void omap_vrfb_adjust_size(u16 *width, u16 *height,
  98. u8 bytespp)
  99. {
  100. *width = ALIGN(*width * bytespp, VRFB_PAGE_WIDTH) / bytespp;
  101. *height = ALIGN(*height, VRFB_PAGE_HEIGHT);
  102. }
  103. EXPORT_SYMBOL(omap_vrfb_adjust_size);
  104. u32 omap_vrfb_min_phys_size(u16 width, u16 height, u8 bytespp)
  105. {
  106. unsigned long image_width_roundup = get_image_width_roundup(width,
  107. bytespp);
  108. if (image_width_roundup > OMAP_VRFB_LINE_LEN)
  109. return 0;
  110. return (width * height * bytespp) + get_extra_physical_size(
  111. image_width_roundup, bytespp);
  112. }
  113. EXPORT_SYMBOL(omap_vrfb_min_phys_size);
  114. u16 omap_vrfb_max_height(u32 phys_size, u16 width, u8 bytespp)
  115. {
  116. unsigned long image_width_roundup = get_image_width_roundup(width,
  117. bytespp);
  118. unsigned long height;
  119. unsigned long extra;
  120. if (image_width_roundup > OMAP_VRFB_LINE_LEN)
  121. return 0;
  122. extra = get_extra_physical_size(image_width_roundup, bytespp);
  123. if (phys_size < extra)
  124. return 0;
  125. height = (phys_size - extra) / (width * bytespp);
  126. /* Virtual views provided by VRFB are limited to 2048x2048. */
  127. return min_t(unsigned long, height, 2048);
  128. }
  129. EXPORT_SYMBOL(omap_vrfb_max_height);
  130. void omap_vrfb_setup(struct vrfb *vrfb, unsigned long paddr,
  131. u16 width, u16 height,
  132. unsigned bytespp, bool yuv_mode)
  133. {
  134. unsigned pixel_size_exp;
  135. u16 vrfb_width;
  136. u16 vrfb_height;
  137. u8 ctx = vrfb->context;
  138. u32 size;
  139. u32 control;
  140. DBG("omapfb_set_vrfb(%d, %lx, %dx%d, %d, %d)\n", ctx, paddr,
  141. width, height, bytespp, yuv_mode);
  142. /* For YUV2 and UYVY modes VRFB needs to handle pixels a bit
  143. * differently. See TRM. */
  144. if (yuv_mode) {
  145. bytespp *= 2;
  146. width /= 2;
  147. }
  148. if (bytespp == 4)
  149. pixel_size_exp = 2;
  150. else if (bytespp == 2)
  151. pixel_size_exp = 1;
  152. else
  153. BUG();
  154. vrfb_width = ALIGN(width * bytespp, VRFB_PAGE_WIDTH) / bytespp;
  155. vrfb_height = ALIGN(height, VRFB_PAGE_HEIGHT);
  156. DBG("vrfb w %u, h %u bytespp %d\n", vrfb_width, vrfb_height, bytespp);
  157. size = vrfb_width << SMS_IMAGEWIDTH_OFFSET;
  158. size |= vrfb_height << SMS_IMAGEHEIGHT_OFFSET;
  159. control = pixel_size_exp << SMS_PS_OFFSET;
  160. control |= VRFB_PAGE_WIDTH_EXP << SMS_PW_OFFSET;
  161. control |= VRFB_PAGE_HEIGHT_EXP << SMS_PH_OFFSET;
  162. vrfb_hw_context[ctx].physical_ba = paddr;
  163. vrfb_hw_context[ctx].size = size;
  164. vrfb_hw_context[ctx].control = control;
  165. omap2_sms_write_rot_physical_ba(paddr, ctx);
  166. omap2_sms_write_rot_size(size, ctx);
  167. omap2_sms_write_rot_control(control, ctx);
  168. DBG("vrfb offset pixels %d, %d\n",
  169. vrfb_width - width, vrfb_height - height);
  170. vrfb->xres = width;
  171. vrfb->yres = height;
  172. vrfb->xoffset = vrfb_width - width;
  173. vrfb->yoffset = vrfb_height - height;
  174. vrfb->bytespp = bytespp;
  175. vrfb->yuv_mode = yuv_mode;
  176. }
  177. EXPORT_SYMBOL(omap_vrfb_setup);
  178. int omap_vrfb_map_angle(struct vrfb *vrfb, u16 height, u8 rot)
  179. {
  180. unsigned long size = height * OMAP_VRFB_LINE_LEN * vrfb->bytespp;
  181. vrfb->vaddr[rot] = ioremap_wc(vrfb->paddr[rot], size);
  182. if (!vrfb->vaddr[rot]) {
  183. printk(KERN_ERR "vrfb: ioremap failed\n");
  184. return -ENOMEM;
  185. }
  186. DBG("ioremapped vrfb area %d of size %lu into %p\n", rot, size,
  187. vrfb->vaddr[rot]);
  188. return 0;
  189. }
  190. EXPORT_SYMBOL(omap_vrfb_map_angle);
  191. void omap_vrfb_release_ctx(struct vrfb *vrfb)
  192. {
  193. int rot;
  194. int ctx = vrfb->context;
  195. if (ctx == 0xff)
  196. return;
  197. DBG("release ctx %d\n", ctx);
  198. mutex_lock(&ctx_lock);
  199. BUG_ON(!(ctx_map & (1 << ctx)));
  200. clear_bit(ctx, &ctx_map);
  201. for (rot = 0; rot < 4; ++rot) {
  202. if (vrfb->paddr[rot]) {
  203. release_mem_region(vrfb->paddr[rot], OMAP_VRFB_SIZE);
  204. vrfb->paddr[rot] = 0;
  205. }
  206. }
  207. vrfb->context = 0xff;
  208. mutex_unlock(&ctx_lock);
  209. }
  210. EXPORT_SYMBOL(omap_vrfb_release_ctx);
  211. int omap_vrfb_request_ctx(struct vrfb *vrfb)
  212. {
  213. int rot;
  214. u32 paddr;
  215. u8 ctx;
  216. int r;
  217. DBG("request ctx\n");
  218. mutex_lock(&ctx_lock);
  219. for (ctx = 0; ctx < VRFB_NUM_CTXS; ++ctx)
  220. if ((ctx_map & (1 << ctx)) == 0)
  221. break;
  222. if (ctx == VRFB_NUM_CTXS) {
  223. pr_err("vrfb: no free contexts\n");
  224. r = -EBUSY;
  225. goto out;
  226. }
  227. DBG("found free ctx %d\n", ctx);
  228. set_bit(ctx, &ctx_map);
  229. memset(vrfb, 0, sizeof(*vrfb));
  230. vrfb->context = ctx;
  231. for (rot = 0; rot < 4; ++rot) {
  232. paddr = SMS_ROT_VIRT_BASE(ctx, rot);
  233. if (!request_mem_region(paddr, OMAP_VRFB_SIZE, "vrfb")) {
  234. pr_err("vrfb: failed to reserve VRFB "
  235. "area for ctx %d, rotation %d\n",
  236. ctx, rot * 90);
  237. omap_vrfb_release_ctx(vrfb);
  238. r = -ENOMEM;
  239. goto out;
  240. }
  241. vrfb->paddr[rot] = paddr;
  242. DBG("VRFB %d/%d: %lx\n", ctx, rot*90, vrfb->paddr[rot]);
  243. }
  244. r = 0;
  245. out:
  246. mutex_unlock(&ctx_lock);
  247. return r;
  248. }
  249. EXPORT_SYMBOL(omap_vrfb_request_ctx);