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