if (err)
                return err;
        memset(fix, 0, sizeof(struct fb_fix_screeninfo));
-       return fbhw->encode_fix(fix, &par);
+       mutex_lock(&info->mm_lock);
+       err = fbhw->encode_fix(fix, &par);
+       mutex_unlock(&info->mm_lock);
+       return err;
 }
 
 static int atafb_get_var(struct fb_var_screeninfo *var, struct fb_info *info)
 
        /* Decode wanted screen parameters */
        fbhw->decode_var(&info->var, par);
+       mutex_lock(&info->mm_lock);
        fbhw->encode_fix(&info->fix, par);
+       mutex_unlock(&info->mm_lock);
 
        /* Set new videomode */
        ata_set_par(par);
 
 
        smem_len = (var->xres_virtual * var->yres_virtual
                    * ((var->bits_per_pixel + 7) / 8));
+       mutex_lock(&info->mm_lock);
        info->fix.smem_len = max(smem_len, sinfo->smem_len);
+       mutex_unlock(&info->mm_lock);
 
        info->screen_base = dma_alloc_writecombine(info->device, info->fix.smem_len,
                                        (dma_addr_t *)&info->fix.smem_start, GFP_KERNEL);
 
 
 static int
 fb_mmap(struct file *file, struct vm_area_struct * vma)
-__acquires(&info->lock)
-__releases(&info->lock)
 {
        int fbidx = iminor(file->f_path.dentry->d_inode);
        struct fb_info *info = registered_fb[fbidx];
        off = vma->vm_pgoff << PAGE_SHIFT;
        if (!fb)
                return -ENODEV;
+       mutex_lock(&info->mm_lock);
        if (fb->fb_mmap) {
                int res;
-               mutex_lock(&info->lock);
                res = fb->fb_mmap(info, vma);
-               mutex_unlock(&info->lock);
+               mutex_unlock(&info->mm_lock);
                return res;
        }
 
-       mutex_lock(&info->lock);
-
        /* frame buffer memory */
        start = info->fix.smem_start;
        len = PAGE_ALIGN((start & ~PAGE_MASK) + info->fix.smem_len);
                /* memory mapped io */
                off -= len;
                if (info->var.accel_flags) {
-                       mutex_unlock(&info->lock);
+                       mutex_unlock(&info->mm_lock);
                        return -EINVAL;
                }
                start = info->fix.mmio_start;
                len = PAGE_ALIGN((start & ~PAGE_MASK) + info->fix.mmio_len);
        }
-       mutex_unlock(&info->lock);
+       mutex_unlock(&info->mm_lock);
        start &= PAGE_MASK;
        if ((vma->vm_end - vma->vm_start + off) > len)
                return -EINVAL;
                        break;
        fb_info->node = i;
        mutex_init(&fb_info->lock);
+       mutex_init(&fb_info->mm_lock);
 
        fb_info->dev = device_create(fb_class, fb_info->device,
                                     MKDEV(FB_MAJOR, i), NULL, "fb%d", i);
 
 static int map_video_memory(struct fb_info *info)
 {
        phys_addr_t phys;
+       u32 smem_len = info->fix.line_length * info->var.yres_virtual;
 
        pr_debug("info->var.xres_virtual = %d\n", info->var.xres_virtual);
        pr_debug("info->var.yres_virtual = %d\n", info->var.yres_virtual);
        pr_debug("info->fix.line_length  = %d\n", info->fix.line_length);
+       pr_debug("MAP_VIDEO_MEMORY: smem_len = %u\n", smem_len);
 
-       info->fix.smem_len = info->fix.line_length * info->var.yres_virtual;
-       pr_debug("MAP_VIDEO_MEMORY: smem_len = %d\n", info->fix.smem_len);
-       info->screen_base = fsl_diu_alloc(info->fix.smem_len, &phys);
+       info->screen_base = fsl_diu_alloc(smem_len, &phys);
        if (info->screen_base == NULL) {
                printk(KERN_ERR "Unable to allocate fb memory\n");
                return -ENOMEM;
        }
+       mutex_lock(&info->mm_lock);
        info->fix.smem_start = (unsigned long) phys;
+       info->fix.smem_len = smem_len;
+       mutex_unlock(&info->mm_lock);
        info->screen_size = info->fix.smem_len;
 
        pr_debug("Allocated fb @ paddr=0x%08lx, size=%d.\n",
-                               info->fix.smem_start,
-               info->fix.smem_len);
+                info->fix.smem_start, info->fix.smem_len);
        pr_debug("screen base %p\n", info->screen_base);
 
        return 0;
 static void unmap_video_memory(struct fb_info *info)
 {
        fsl_diu_free(info->screen_base, info->fix.smem_len);
+       mutex_lock(&info->mm_lock);
        info->screen_base = NULL;
        info->fix.smem_start = 0;
        info->fix.smem_len = 0;
+       mutex_unlock(&info->mm_lock);
 }
 
 /*
 
        memset(fix, 0, sizeof(struct fb_fix_screeninfo));
 
        strcpy(fix->id, "I810");
+       mutex_lock(&info->mm_lock);
        fix->smem_start = par->fb.physical;
        fix->smem_len = par->fb.size;
+       mutex_unlock(&info->mm_lock);
        fix->type = FB_TYPE_PACKED_PIXELS;
        fix->type_aux = 0;
        fix->xpanstep = 8;
 
        struct fb_fix_screeninfo *fix = &ACCESS_FBINFO(fbcon).fix;
        DBG(__func__)
 
+       mutex_lock(&ACCESS_FBINFO(fbcon).mm_lock);
        fix->smem_start = ACCESS_FBINFO(video.base) + ACCESS_FBINFO(curr.ydstorg.bytes);
        fix->smem_len = ACCESS_FBINFO(video.len_usable) - ACCESS_FBINFO(curr.ydstorg.bytes);
+       mutex_unlock(&ACCESS_FBINFO(fbcon).mm_lock);
 }
 
 static int matroxfb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
        spin_lock_init(&ACCESS_FBINFO(lock.accel));
        init_rwsem(&ACCESS_FBINFO(crtc2.lock));
        init_rwsem(&ACCESS_FBINFO(altout.lock));
+       mutex_init(&ACCESS_FBINFO(fbcon).mm_lock);
        ACCESS_FBINFO(irq_flags) = 0;
        init_waitqueue_head(&ACCESS_FBINFO(crtc1.vsync.wait));
        init_waitqueue_head(&ACCESS_FBINFO(crtc2.vsync.wait));
 
 #undef m2info
 }
 
-static void matroxfb_dh_init_fix(struct matroxfb_dh_fb_info *m2info) {
+static void matroxfb_dh_init_fix(struct matroxfb_dh_fb_info *m2info)
+{
        struct fb_fix_screeninfo *fix = &m2info->fbcon.fix;
 
        strcpy(fix->id, "MATROX DH");
 
+       mutex_lock(&m2info->fbcon.mm_lock);
        fix->smem_start = m2info->video.base;
        fix->smem_len = m2info->video.len_usable;
+       mutex_unlock(&m2info->fbcon.mm_lock);
        fix->ypanstep = 1;
        fix->ywrapstep = 0;
        fix->xpanstep = 8;      /* TBD */
 
 }
 
 static int mx3fb_blank(int blank, struct fb_info *fbi);
-static int mx3fb_map_video_memory(struct fb_info *fbi);
+static int mx3fb_map_video_memory(struct fb_info *fbi, unsigned int mem_len);
 static int mx3fb_unmap_video_memory(struct fb_info *fbi);
 
 /**
                if (fbi->fix.smem_start)
                        mx3fb_unmap_video_memory(fbi);
 
-               fbi->fix.smem_len = mem_len;
-               if (mx3fb_map_video_memory(fbi) < 0) {
+               if (mx3fb_map_video_memory(fbi, mem_len) < 0) {
                        mutex_unlock(&mx3_fbi->mutex);
                        return -ENOMEM;
                }
 /**
  * mx3fb_map_video_memory() - allocates the DRAM memory for the frame buffer.
  * @fbi:       framebuffer information pointer
+ * @mem_len:   length of mapped memory
  * @return:    Error code indicating success or failure
  *
  * This buffer is remapped into a non-cached, non-buffered, memory region to
  * area is remapped, all virtual memory access to the video memory should occur
  * at the new region.
  */
-static int mx3fb_map_video_memory(struct fb_info *fbi)
+static int mx3fb_map_video_memory(struct fb_info *fbi, unsigned int mem_len)
 {
        int retval = 0;
        dma_addr_t addr;
 
        fbi->screen_base = dma_alloc_writecombine(fbi->device,
-                                                 fbi->fix.smem_len,
+                                                 mem_len,
                                                  &addr, GFP_DMA);
 
        if (!fbi->screen_base) {
                dev_err(fbi->device, "Cannot allocate %u bytes framebuffer memory\n",
-                       fbi->fix.smem_len);
+                       mem_len);
                retval = -EBUSY;
                goto err0;
        }
 
+       mutex_lock(&fbi->mm_lock);
        fbi->fix.smem_start = addr;
+       fbi->fix.smem_len = mem_len;
+       mutex_unlock(&fbi->mm_lock);
 
        dev_dbg(fbi->device, "allocated fb @ p=0x%08x, v=0x%p, size=%d.\n",
                (uint32_t) fbi->fix.smem_start, fbi->screen_base, fbi->fix.smem_len);
                              fbi->screen_base, fbi->fix.smem_start);
 
        fbi->screen_base = 0;
+       mutex_lock(&fbi->mm_lock);
        fbi->fix.smem_start = 0;
        fbi->fix.smem_len = 0;
+       mutex_unlock(&fbi->mm_lock);
        return 0;
 }
 
 
 
        rg = &plane->fbdev->mem_desc.region[plane->idx];
        fbi->screen_base        = rg->vaddr;
+       mutex_lock(&fbi->mm_lock);
        fix->smem_start         = rg->paddr;
        fix->smem_len           = rg->size;
+       mutex_unlock(&fbi->mm_lock);
 
        fix->type = FB_TYPE_PACKED_PIXELS;
        bpp = var->bits_per_pixel;
                                 * plane memory is dealloce'd, the other
                                 * screen parameters in var / fix are invalid.
                                 */
+                               mutex_lock(&fbi->mm_lock);
                                fbi->fix.smem_start = 0;
                                fbi->fix.smem_len = 0;
+                               mutex_unlock(&fbi->mm_lock);
                        }
                }
        }
 
                offset = 0x10;
 
        info->screen_base = pinfo->frame_buffer + init->fb_offset + offset;
+       mutex_lock(&info->mm_lock);
        info->fix.smem_start = (pinfo->frame_buffer_phys) + init->fb_offset + offset;
+       mutex_unlock(&info->mm_lock);
        info->fix.visual = (pinfo->cmode == CMODE_8) ?
                FB_VISUAL_PSEUDOCOLOR : FB_VISUAL_DIRECTCOLOR;
        info->fix.line_length = vmode_attrs[pinfo->vmode-1].hres * (1<<pinfo->cmode)
 
        ofb->video_mem_phys = virt_to_phys(ofb->video_mem);
        ofb->video_mem_size = size;
 
+       mutex_lock(&ofb->fb.mm_lock);
        ofb->fb.fix.smem_start  = ofb->video_mem_phys;
        ofb->fb.fix.smem_len    = ofb->fb.fix.line_length * var->yres_virtual;
+       mutex_unlock(&ofb->fb.mm_lock);
        ofb->fb.screen_base     = ofb->video_mem;
        return 0;
 }
 
        return 0;
 }
 
-static void encode_fix(struct fb_fix_screeninfo *fix, struct fb_info *info,
-                      unsigned long stride)
-{
-       memset(fix, 0, sizeof(struct fb_fix_screeninfo));
-       strcpy(fix->id, "sh7760-lcdc");
-
-       fix->smem_start = (unsigned long)info->screen_base;
-       fix->smem_len = info->screen_size;
-
-       fix->line_length = stride;
-}
-
 static int sh7760fb_get_color_info(struct device *dev,
                                   u16 lddfr, int *bpp, int *gray)
 {
 
        iowrite32(ldsarl, par->base + LDSARL);  /* mem for lower half of DSTN */
 
-       encode_fix(&info->fix, info, stride);
+       info->fix.line_length = stride;
+
        sh7760fb_check_var(&info->var, info);
 
        sh7760fb_blank(FB_BLANK_UNBLANK, info); /* panel on! */
 
        info->screen_base = fbmem;
        info->screen_size = vram;
+       info->fix.smem_start = (unsigned long)info->screen_base;
+       info->fix.smem_len = info->screen_size;
 
        return 0;
 }
        info->var.transp.length = 0;
        info->var.transp.msb_right = 0;
 
+       strcpy(info->fix.id, "sh7760-lcdc");
+
        /* set the DON2 bit now, before cmap allocation, as it will randomize
         * palette memory.
         */
 
 
        strcpy(fix->id, ivideo->myid);
 
+       mutex_lock(&info->mm_lock);
        fix->smem_start  = ivideo->video_base + ivideo->video_offset;
        fix->smem_len    = ivideo->sisfb_mem;
+       mutex_unlock(&info->mm_lock);
        fix->type        = FB_TYPE_PACKED_PIXELS;
        fix->type_aux    = 0;
        fix->visual      = (ivideo->video_bpp == 8) ? FB_VISUAL_PSEUDOCOLOR : FB_VISUAL_TRUECOLOR;
 
 #define SM501_MEMF_ACCEL               (8)
 
 static int sm501_alloc_mem(struct sm501fb_info *inf, struct sm501_mem *mem,
-                          unsigned int why, size_t size)
+                          unsigned int why, size_t size, u32 smem_len)
 {
        struct sm501fb_par *par;
        struct fb_info *fbi;
                if (ptr > 0)
                        ptr &= ~(PAGE_SIZE - 1);
 
-               if (fbi && ptr < fbi->fix.smem_len)
+               if (fbi && ptr < smem_len)
                        return -ENOMEM;
 
                break;
 
        case SM501_MEMF_ACCEL:
                fbi = inf->fb[HEAD_CRT];
-               ptr = fbi ? fbi->fix.smem_len : 0;
+               ptr = fbi ? smem_len : 0;
 
                fbi = inf->fb[HEAD_PANEL];
                if (fbi) {
        unsigned int mem_type;
        unsigned int clock_type;
        unsigned int head_addr;
+       unsigned int smem_len;
 
        dev_dbg(fbi->dev, "%s: %dx%d, bpp = %d, virtual %dx%d\n",
                __func__, var->xres, var->yres, var->bits_per_pixel,
 
        /* allocate fb memory within 501 */
        info->fix.line_length = (var->xres_virtual * var->bits_per_pixel)/8;
-       info->fix.smem_len    = info->fix.line_length * var->yres_virtual;
+       smem_len = info->fix.line_length * var->yres_virtual;
 
        dev_dbg(fbi->dev, "%s: line length = %u\n", __func__,
                info->fix.line_length);
 
-       if (sm501_alloc_mem(fbi, &par->screen, mem_type,
-                           info->fix.smem_len)) {
+       if (sm501_alloc_mem(fbi, &par->screen, mem_type, smem_len, smem_len)) {
                dev_err(fbi->dev, "no memory available\n");
                return -ENOMEM;
        }
 
+       mutex_lock(&info->mm_lock);
        info->fix.smem_start = fbi->fbmem_res->start + par->screen.sm_addr;
+       info->fix.smem_len   = smem_len;
+       mutex_unlock(&info->mm_lock);
 
        info->screen_base = fbi->fbmem + par->screen.sm_addr;
        info->screen_size = info->fix.smem_len;
        if ((control & SM501_DC_CRT_CONTROL_SEL) == 0) {
                /* the head is displaying panel data... */
 
-               sm501_alloc_mem(fbi, &par->screen, SM501_MEMF_CRT, 0);
+               sm501_alloc_mem(fbi, &par->screen, SM501_MEMF_CRT, 0,
+                               info->fix.smem_len);
                goto out_update;
        }
 
 
        par->cursor_regs = info->regs + reg_base;
 
-       ret = sm501_alloc_mem(info, &par->cursor, SM501_MEMF_CURSOR, 1024);
+       ret = sm501_alloc_mem(info, &par->cursor, SM501_MEMF_CURSOR, 1024,
+                             fbi->fix.smem_len);
        if (ret < 0)
                return ret;
 
 
                info->fix.ywrapstep = 0;
                info->fix.line_length = par->xres * BITS_PER_PIXEL / 8;
 
+               mutex_lock(&info->mm_lock);
                if ((par->xres*par->yres*BITS_PER_PIXEL/8) > (MEM_INT_SIZE+1)) {
                        par->extmem_active = 1;
                        info->fix.smem_len = par->mach->mem->size+1;
                        par->extmem_active = 0;
                        info->fix.smem_len = MEM_INT_SIZE+1;
                }
+               mutex_unlock(&info->mm_lock);
 
                w100fb_activate_var(par);
        }
 
        int node;
        int flags;
        struct mutex lock;              /* Lock for open/release/ioctl funcs */
+       struct mutex mm_lock;           /* Lock for fb_mmap and smem_* fields */
        struct fb_var_screeninfo var;   /* Current var */
        struct fb_fix_screeninfo fix;   /* Current fix */
        struct fb_monspecs monspecs;    /* Current Monitor specs */