2009-12-10 08:19:58 +08:00
|
|
|
/**************************************************************************
|
|
|
|
*
|
2011-08-31 15:42:55 +08:00
|
|
|
* Copyright © 2009-2011 VMware, Inc., Palo Alto, CA., USA
|
2009-12-10 08:19:58 +08:00
|
|
|
* All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
* copy of this software and associated documentation files (the
|
|
|
|
* "Software"), to deal in the Software without restriction, including
|
|
|
|
* without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
* distribute, sub license, and/or sell copies of the Software, and to
|
|
|
|
* permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
* the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice (including the
|
|
|
|
* next paragraph) shall be included in all copies or substantial portions
|
|
|
|
* of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
|
|
|
|
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
|
|
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
|
|
|
* USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*
|
|
|
|
**************************************************************************/
|
|
|
|
|
|
|
|
#include "vmwgfx_drv.h"
|
2012-10-03 01:01:07 +08:00
|
|
|
#include <drm/drmP.h>
|
|
|
|
#include <drm/ttm/ttm_bo_driver.h>
|
2009-12-10 08:19:58 +08:00
|
|
|
|
2013-08-29 08:32:53 +08:00
|
|
|
#define VMW_PPN_SIZE (sizeof(unsigned long))
|
|
|
|
/* A future safe maximum remap size. */
|
|
|
|
#define VMW_PPN_PER_REMAP ((31 * 1024) / VMW_PPN_SIZE)
|
2013-10-24 16:49:26 +08:00
|
|
|
#define DMA_ADDR_INVALID ((dma_addr_t) 0)
|
|
|
|
#define DMA_PAGE_INVALID 0UL
|
2011-08-31 15:42:55 +08:00
|
|
|
|
|
|
|
static int vmw_gmr2_bind(struct vmw_private *dev_priv,
|
2013-10-24 16:49:26 +08:00
|
|
|
struct vmw_piter *iter,
|
2011-08-31 15:42:55 +08:00
|
|
|
unsigned long num_pages,
|
|
|
|
int gmr_id)
|
|
|
|
{
|
|
|
|
SVGAFifoCmdDefineGMR2 define_cmd;
|
|
|
|
SVGAFifoCmdRemapGMR2 remap_cmd;
|
|
|
|
uint32_t *cmd;
|
|
|
|
uint32_t *cmd_orig;
|
2013-08-29 08:32:53 +08:00
|
|
|
uint32_t define_size = sizeof(define_cmd) + sizeof(*cmd);
|
|
|
|
uint32_t remap_num = num_pages / VMW_PPN_PER_REMAP + ((num_pages % VMW_PPN_PER_REMAP) > 0);
|
|
|
|
uint32_t remap_size = VMW_PPN_SIZE * num_pages + (sizeof(remap_cmd) + sizeof(*cmd)) * remap_num;
|
|
|
|
uint32_t remap_pos = 0;
|
|
|
|
uint32_t cmd_size = define_size + remap_size;
|
2011-08-31 15:42:55 +08:00
|
|
|
uint32_t i;
|
|
|
|
|
2013-08-29 08:32:53 +08:00
|
|
|
cmd_orig = cmd = vmw_fifo_reserve(dev_priv, cmd_size);
|
2011-08-31 15:42:55 +08:00
|
|
|
if (unlikely(cmd == NULL))
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
define_cmd.gmrId = gmr_id;
|
|
|
|
define_cmd.numPages = num_pages;
|
|
|
|
|
2013-08-29 08:32:53 +08:00
|
|
|
*cmd++ = SVGA_CMD_DEFINE_GMR2;
|
|
|
|
memcpy(cmd, &define_cmd, sizeof(define_cmd));
|
|
|
|
cmd += sizeof(define_cmd) / sizeof(*cmd);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Need to split the command if there are too many
|
|
|
|
* pages that goes into the gmr.
|
|
|
|
*/
|
|
|
|
|
2011-08-31 15:42:55 +08:00
|
|
|
remap_cmd.gmrId = gmr_id;
|
|
|
|
remap_cmd.flags = (VMW_PPN_SIZE > sizeof(*cmd)) ?
|
|
|
|
SVGA_REMAP_GMR2_PPN64 : SVGA_REMAP_GMR2_PPN32;
|
|
|
|
|
2013-08-29 08:32:53 +08:00
|
|
|
while (num_pages > 0) {
|
|
|
|
unsigned long nr = min(num_pages, (unsigned long)VMW_PPN_PER_REMAP);
|
|
|
|
|
|
|
|
remap_cmd.offsetPages = remap_pos;
|
|
|
|
remap_cmd.numPages = nr;
|
2011-08-31 15:42:55 +08:00
|
|
|
|
2013-08-29 08:32:53 +08:00
|
|
|
*cmd++ = SVGA_CMD_REMAP_GMR2;
|
|
|
|
memcpy(cmd, &remap_cmd, sizeof(remap_cmd));
|
|
|
|
cmd += sizeof(remap_cmd) / sizeof(*cmd);
|
2011-08-31 15:42:55 +08:00
|
|
|
|
2013-08-29 08:32:53 +08:00
|
|
|
for (i = 0; i < nr; ++i) {
|
|
|
|
if (VMW_PPN_SIZE <= 4)
|
2013-10-24 16:49:26 +08:00
|
|
|
*cmd = vmw_piter_dma_addr(iter) >> PAGE_SHIFT;
|
2013-08-29 08:32:53 +08:00
|
|
|
else
|
2013-10-24 16:49:26 +08:00
|
|
|
*((uint64_t *)cmd) = vmw_piter_dma_addr(iter) >>
|
|
|
|
PAGE_SHIFT;
|
2011-08-31 15:42:55 +08:00
|
|
|
|
2013-08-29 08:32:53 +08:00
|
|
|
cmd += VMW_PPN_SIZE / sizeof(*cmd);
|
2013-10-24 16:49:26 +08:00
|
|
|
vmw_piter_next(iter);
|
2013-08-29 08:32:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
num_pages -= nr;
|
|
|
|
remap_pos += nr;
|
2011-08-31 15:42:55 +08:00
|
|
|
}
|
|
|
|
|
2013-08-29 08:32:53 +08:00
|
|
|
BUG_ON(cmd != cmd_orig + cmd_size / sizeof(*cmd));
|
|
|
|
|
|
|
|
vmw_fifo_commit(dev_priv, cmd_size);
|
2011-08-31 15:42:55 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void vmw_gmr2_unbind(struct vmw_private *dev_priv,
|
|
|
|
int gmr_id)
|
|
|
|
{
|
|
|
|
SVGAFifoCmdDefineGMR2 define_cmd;
|
|
|
|
uint32_t define_size = sizeof(define_cmd) + 4;
|
|
|
|
uint32_t *cmd;
|
|
|
|
|
|
|
|
cmd = vmw_fifo_reserve(dev_priv, define_size);
|
|
|
|
if (unlikely(cmd == NULL)) {
|
|
|
|
DRM_ERROR("GMR2 unbind failed.\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
define_cmd.gmrId = gmr_id;
|
|
|
|
define_cmd.numPages = 0;
|
|
|
|
|
|
|
|
*cmd++ = SVGA_CMD_DEFINE_GMR2;
|
|
|
|
memcpy(cmd, &define_cmd, sizeof(define_cmd));
|
|
|
|
|
|
|
|
vmw_fifo_commit(dev_priv, define_size);
|
|
|
|
}
|
|
|
|
|
2013-10-24 16:49:26 +08:00
|
|
|
|
|
|
|
static void vmw_gmr_free_descriptors(struct device *dev, dma_addr_t desc_dma,
|
|
|
|
struct list_head *desc_pages)
|
|
|
|
{
|
|
|
|
struct page *page, *next;
|
|
|
|
struct svga_guest_mem_descriptor *page_virtual;
|
|
|
|
unsigned int desc_per_page = PAGE_SIZE /
|
|
|
|
sizeof(struct svga_guest_mem_descriptor) - 1;
|
|
|
|
|
|
|
|
if (list_empty(desc_pages))
|
|
|
|
return;
|
|
|
|
|
|
|
|
list_for_each_entry_safe(page, next, desc_pages, lru) {
|
|
|
|
list_del_init(&page->lru);
|
|
|
|
|
|
|
|
if (likely(desc_dma != DMA_ADDR_INVALID)) {
|
|
|
|
dma_unmap_page(dev, desc_dma, PAGE_SIZE,
|
|
|
|
DMA_TO_DEVICE);
|
|
|
|
}
|
|
|
|
|
|
|
|
page_virtual = kmap_atomic(page);
|
|
|
|
desc_dma = page_virtual[desc_per_page].ppn << PAGE_SHIFT;
|
|
|
|
kunmap_atomic(page_virtual);
|
|
|
|
|
|
|
|
__free_page(page);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-12-10 08:19:58 +08:00
|
|
|
/**
|
|
|
|
* FIXME: Adjust to the ttm lowmem / highmem storage to minimize
|
|
|
|
* the number of used descriptors.
|
2013-10-24 16:49:26 +08:00
|
|
|
*
|
2009-12-10 08:19:58 +08:00
|
|
|
*/
|
|
|
|
|
2013-10-24 16:49:26 +08:00
|
|
|
static int vmw_gmr_build_descriptors(struct device *dev,
|
|
|
|
struct list_head *desc_pages,
|
|
|
|
struct vmw_piter *iter,
|
|
|
|
unsigned long num_pages,
|
|
|
|
dma_addr_t *first_dma)
|
2009-12-10 08:19:58 +08:00
|
|
|
{
|
2013-10-24 16:49:26 +08:00
|
|
|
struct page *page;
|
2009-12-10 08:19:58 +08:00
|
|
|
struct svga_guest_mem_descriptor *page_virtual = NULL;
|
|
|
|
struct svga_guest_mem_descriptor *desc_virtual = NULL;
|
|
|
|
unsigned int desc_per_page;
|
|
|
|
unsigned long prev_pfn;
|
|
|
|
unsigned long pfn;
|
|
|
|
int ret;
|
2013-10-24 16:49:26 +08:00
|
|
|
dma_addr_t desc_dma;
|
2009-12-10 08:19:58 +08:00
|
|
|
|
|
|
|
desc_per_page = PAGE_SIZE /
|
|
|
|
sizeof(struct svga_guest_mem_descriptor) - 1;
|
|
|
|
|
|
|
|
while (likely(num_pages != 0)) {
|
|
|
|
page = alloc_page(__GFP_HIGHMEM);
|
|
|
|
if (unlikely(page == NULL)) {
|
|
|
|
ret = -ENOMEM;
|
|
|
|
goto out_err;
|
|
|
|
}
|
|
|
|
|
|
|
|
list_add_tail(&page->lru, desc_pages);
|
2011-11-25 23:14:20 +08:00
|
|
|
page_virtual = kmap_atomic(page);
|
2009-12-10 08:19:58 +08:00
|
|
|
desc_virtual = page_virtual - 1;
|
|
|
|
prev_pfn = ~(0UL);
|
|
|
|
|
|
|
|
while (likely(num_pages != 0)) {
|
2013-10-24 16:49:26 +08:00
|
|
|
pfn = vmw_piter_dma_addr(iter) >> PAGE_SHIFT;
|
2009-12-10 08:19:58 +08:00
|
|
|
|
|
|
|
if (pfn != prev_pfn + 1) {
|
|
|
|
|
|
|
|
if (desc_virtual - page_virtual ==
|
|
|
|
desc_per_page - 1)
|
|
|
|
break;
|
|
|
|
|
|
|
|
(++desc_virtual)->ppn = cpu_to_le32(pfn);
|
|
|
|
desc_virtual->num_pages = cpu_to_le32(1);
|
|
|
|
} else {
|
|
|
|
uint32_t tmp =
|
|
|
|
le32_to_cpu(desc_virtual->num_pages);
|
|
|
|
desc_virtual->num_pages = cpu_to_le32(tmp + 1);
|
|
|
|
}
|
|
|
|
prev_pfn = pfn;
|
|
|
|
--num_pages;
|
2013-10-24 16:49:26 +08:00
|
|
|
vmw_piter_next(iter);
|
2009-12-10 08:19:58 +08:00
|
|
|
}
|
|
|
|
|
2013-10-24 16:49:26 +08:00
|
|
|
(++desc_virtual)->ppn = DMA_PAGE_INVALID;
|
2009-12-10 08:19:58 +08:00
|
|
|
desc_virtual->num_pages = cpu_to_le32(0);
|
2013-10-24 16:49:26 +08:00
|
|
|
kunmap_atomic(page_virtual);
|
2009-12-10 08:19:58 +08:00
|
|
|
}
|
|
|
|
|
2013-10-24 16:49:26 +08:00
|
|
|
desc_dma = 0;
|
|
|
|
list_for_each_entry_reverse(page, desc_pages, lru) {
|
|
|
|
page_virtual = kmap_atomic(page);
|
|
|
|
page_virtual[desc_per_page].ppn = desc_dma >> PAGE_SHIFT;
|
2011-11-25 23:14:20 +08:00
|
|
|
kunmap_atomic(page_virtual);
|
2013-10-24 16:49:26 +08:00
|
|
|
desc_dma = dma_map_page(dev, page, 0, PAGE_SIZE,
|
|
|
|
DMA_TO_DEVICE);
|
|
|
|
|
|
|
|
if (unlikely(dma_mapping_error(dev, desc_dma)))
|
|
|
|
goto out_err;
|
|
|
|
}
|
|
|
|
*first_dma = desc_dma;
|
2009-12-10 08:19:58 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
out_err:
|
2013-10-24 16:49:26 +08:00
|
|
|
vmw_gmr_free_descriptors(dev, DMA_ADDR_INVALID, desc_pages);
|
2009-12-10 08:19:58 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void vmw_gmr_fire_descriptors(struct vmw_private *dev_priv,
|
2013-10-24 16:49:26 +08:00
|
|
|
int gmr_id, dma_addr_t desc_dma)
|
2009-12-10 08:19:58 +08:00
|
|
|
{
|
|
|
|
mutex_lock(&dev_priv->hw_mutex);
|
|
|
|
|
|
|
|
vmw_write(dev_priv, SVGA_REG_GMR_ID, gmr_id);
|
|
|
|
wmb();
|
2013-10-24 16:49:26 +08:00
|
|
|
vmw_write(dev_priv, SVGA_REG_GMR_DESCRIPTOR, desc_dma >> PAGE_SHIFT);
|
2009-12-10 08:19:58 +08:00
|
|
|
mb();
|
|
|
|
|
|
|
|
mutex_unlock(&dev_priv->hw_mutex);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
int vmw_gmr_bind(struct vmw_private *dev_priv,
|
2013-10-24 16:49:26 +08:00
|
|
|
const struct vmw_sg_table *vsgt,
|
2010-10-27 03:21:47 +08:00
|
|
|
unsigned long num_pages,
|
|
|
|
int gmr_id)
|
2009-12-10 08:19:58 +08:00
|
|
|
{
|
|
|
|
struct list_head desc_pages;
|
2013-10-24 16:49:26 +08:00
|
|
|
dma_addr_t desc_dma = 0;
|
|
|
|
struct device *dev = dev_priv->dev->dev;
|
|
|
|
struct vmw_piter data_iter;
|
2010-10-27 03:21:47 +08:00
|
|
|
int ret;
|
2009-12-10 08:19:58 +08:00
|
|
|
|
2013-10-24 16:49:26 +08:00
|
|
|
vmw_piter_start(&data_iter, vsgt, 0);
|
|
|
|
|
|
|
|
if (unlikely(!vmw_piter_next(&data_iter)))
|
|
|
|
return 0;
|
|
|
|
|
2011-08-31 15:42:55 +08:00
|
|
|
if (likely(dev_priv->capabilities & SVGA_CAP_GMR2))
|
2013-10-24 16:49:26 +08:00
|
|
|
return vmw_gmr2_bind(dev_priv, &data_iter, num_pages, gmr_id);
|
2011-08-31 15:42:55 +08:00
|
|
|
|
2010-10-27 03:21:47 +08:00
|
|
|
if (unlikely(!(dev_priv->capabilities & SVGA_CAP_GMR)))
|
2009-12-10 08:19:58 +08:00
|
|
|
return -EINVAL;
|
|
|
|
|
2013-10-24 16:49:26 +08:00
|
|
|
if (vsgt->num_regions > dev_priv->max_gmr_descriptors)
|
2009-12-10 08:19:58 +08:00
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
INIT_LIST_HEAD(&desc_pages);
|
|
|
|
|
2013-10-24 16:49:26 +08:00
|
|
|
ret = vmw_gmr_build_descriptors(dev, &desc_pages, &data_iter,
|
|
|
|
num_pages, &desc_dma);
|
2009-12-10 08:19:58 +08:00
|
|
|
if (unlikely(ret != 0))
|
2010-10-27 03:21:47 +08:00
|
|
|
return ret;
|
2009-12-10 08:19:58 +08:00
|
|
|
|
2013-10-24 16:49:26 +08:00
|
|
|
vmw_gmr_fire_descriptors(dev_priv, gmr_id, desc_dma);
|
|
|
|
vmw_gmr_free_descriptors(dev, desc_dma, &desc_pages);
|
2009-12-10 08:19:58 +08:00
|
|
|
|
2010-10-27 03:21:47 +08:00
|
|
|
return 0;
|
2009-12-10 08:19:58 +08:00
|
|
|
}
|
|
|
|
|
2010-10-27 03:21:47 +08:00
|
|
|
|
2009-12-10 08:19:58 +08:00
|
|
|
void vmw_gmr_unbind(struct vmw_private *dev_priv, int gmr_id)
|
|
|
|
{
|
2011-08-31 15:42:55 +08:00
|
|
|
if (likely(dev_priv->capabilities & SVGA_CAP_GMR2)) {
|
|
|
|
vmw_gmr2_unbind(dev_priv, gmr_id);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-12-10 08:19:58 +08:00
|
|
|
mutex_lock(&dev_priv->hw_mutex);
|
|
|
|
vmw_write(dev_priv, SVGA_REG_GMR_ID, gmr_id);
|
|
|
|
wmb();
|
|
|
|
vmw_write(dev_priv, SVGA_REG_GMR_DESCRIPTOR, 0);
|
|
|
|
mb();
|
|
|
|
mutex_unlock(&dev_priv->hw_mutex);
|
|
|
|
}
|