| 1 |
// SPDX-License-Identifier: GPL-2.0 |
| 2 |
/* Copyright(c) 2023 Advanced Micro Devices, Inc. */ |
| 3 |
|
| 4 |
#include <linux/interval_tree.h> |
| 5 |
#include <linux/vfio.h> |
| 6 |
#include <linux/vmalloc.h> |
| 7 |
|
| 8 |
#include <linux/pds/pds_common.h> |
| 9 |
#include <linux/pds/pds_core_if.h> |
| 10 |
#include <linux/pds/pds_adminq.h> |
| 11 |
|
| 12 |
#include "vfio_dev.h" |
| 13 |
#include "cmds.h" |
| 14 |
#include "dirty.h" |
| 15 |
|
| 16 |
#define READ_SEQ true |
| 17 |
#define WRITE_ACK false |
| 18 |
|
| 19 |
bool pds_vfio_dirty_is_enabled(struct pds_vfio_pci_device *pds_vfio) |
| 20 |
{ |
| 21 |
return pds_vfio->dirty.is_enabled; |
| 22 |
} |
| 23 |
|
| 24 |
void pds_vfio_dirty_set_enabled(struct pds_vfio_pci_device *pds_vfio) |
| 25 |
{ |
| 26 |
pds_vfio->dirty.is_enabled = true; |
| 27 |
} |
| 28 |
|
| 29 |
void pds_vfio_dirty_set_disabled(struct pds_vfio_pci_device *pds_vfio) |
| 30 |
{ |
| 31 |
pds_vfio->dirty.is_enabled = false; |
| 32 |
} |
| 33 |
|
| 34 |
static void |
| 35 |
pds_vfio_print_guest_region_info(struct pds_vfio_pci_device *pds_vfio, |
| 36 |
u8 max_regions) |
| 37 |
{ |
| 38 |
int len = max_regions * sizeof(struct pds_lm_dirty_region_info); |
| 39 |
struct pci_dev *pdev = pds_vfio->vfio_coredev.pdev; |
| 40 |
struct device *pdsc_dev = &pci_physfn(pdev)->dev; |
| 41 |
struct pds_lm_dirty_region_info *region_info; |
| 42 |
dma_addr_t regions_dma; |
| 43 |
u8 num_regions; |
| 44 |
int err; |
| 45 |
|
| 46 |
region_info = kzalloc_objs(struct pds_lm_dirty_region_info, max_regions); |
| 47 |
if (!region_info) |
| 48 |
return; |
| 49 |
|
| 50 |
regions_dma = |
| 51 |
dma_map_single(pdsc_dev, region_info, len, DMA_FROM_DEVICE); |
| 52 |
if (dma_mapping_error(pdsc_dev, regions_dma)) |
| 53 |
goto out_free_region_info; |
| 54 |
|
| 55 |
err = pds_vfio_dirty_status_cmd(pds_vfio, regions_dma, &max_regions, |
| 56 |
&num_regions); |
| 57 |
dma_unmap_single(pdsc_dev, regions_dma, len, DMA_FROM_DEVICE); |
| 58 |
if (err) |
| 59 |
goto out_free_region_info; |
| 60 |
|
| 61 |
for (unsigned int i = 0; i < num_regions; i++) |
| 62 |
dev_dbg(&pdev->dev, |
| 63 |
"region_info[%d]: dma_base 0x%llx page_count %u page_size_log2 %u\n", |
| 64 |
i, le64_to_cpu(region_info[i].dma_base), |
| 65 |
le32_to_cpu(region_info[i].page_count), |
| 66 |
region_info[i].page_size_log2); |
| 67 |
|
| 68 |
out_free_region_info: |
| 69 |
kfree(region_info); |
| 70 |
} |
| 71 |
|
| 72 |
static int pds_vfio_dirty_alloc_bitmaps(struct pds_vfio_region *region, |
| 73 |
unsigned long bytes) |
| 74 |
{ |
| 75 |
unsigned long *host_seq_bmp, *host_ack_bmp; |
| 76 |
|
| 77 |
host_seq_bmp = vzalloc(bytes); |
| 78 |
if (!host_seq_bmp) |
| 79 |
return -ENOMEM; |
| 80 |
|
| 81 |
host_ack_bmp = vzalloc(bytes); |
| 82 |
if (!host_ack_bmp) { |
| 83 |
vfree(host_seq_bmp); |
| 84 |
return -ENOMEM; |
| 85 |
} |
| 86 |
|
| 87 |
region->host_seq = host_seq_bmp; |
| 88 |
region->host_ack = host_ack_bmp; |
| 89 |
region->bmp_bytes = bytes; |
| 90 |
|
| 91 |
return 0; |
| 92 |
} |
| 93 |
|
| 94 |
static void pds_vfio_dirty_free_bitmaps(struct pds_vfio_dirty *dirty) |
| 95 |
{ |
| 96 |
if (!dirty->regions) |
| 97 |
return; |
| 98 |
|
| 99 |
for (int i = 0; i < dirty->num_regions; i++) { |
| 100 |
struct pds_vfio_region *region = &dirty->regions[i]; |
| 101 |
|
| 102 |
vfree(region->host_seq); |
| 103 |
vfree(region->host_ack); |
| 104 |
region->host_seq = NULL; |
| 105 |
region->host_ack = NULL; |
| 106 |
region->bmp_bytes = 0; |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
static void __pds_vfio_dirty_free_sgl(struct pds_vfio_pci_device *pds_vfio, |
| 111 |
struct pds_vfio_region *region) |
| 112 |
{ |
| 113 |
struct pci_dev *pdev = pds_vfio->vfio_coredev.pdev; |
| 114 |
struct device *pdsc_dev = &pci_physfn(pdev)->dev; |
| 115 |
|
| 116 |
dma_unmap_single(pdsc_dev, region->sgl_addr, |
| 117 |
region->num_sge * sizeof(struct pds_lm_sg_elem), |
| 118 |
DMA_BIDIRECTIONAL); |
| 119 |
kfree(region->sgl); |
| 120 |
|
| 121 |
region->num_sge = 0; |
| 122 |
region->sgl = NULL; |
| 123 |
region->sgl_addr = 0; |
| 124 |
} |
| 125 |
|
| 126 |
static void pds_vfio_dirty_free_sgl(struct pds_vfio_pci_device *pds_vfio) |
| 127 |
{ |
| 128 |
struct pds_vfio_dirty *dirty = &pds_vfio->dirty; |
| 129 |
|
| 130 |
if (!dirty->regions) |
| 131 |
return; |
| 132 |
|
| 133 |
for (int i = 0; i < dirty->num_regions; i++) { |
| 134 |
struct pds_vfio_region *region = &dirty->regions[i]; |
| 135 |
|
| 136 |
if (region->sgl) |
| 137 |
__pds_vfio_dirty_free_sgl(pds_vfio, region); |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
static int pds_vfio_dirty_alloc_sgl(struct pds_vfio_pci_device *pds_vfio, |
| 142 |
struct pds_vfio_region *region, |
| 143 |
u32 page_count) |
| 144 |
{ |
| 145 |
struct pci_dev *pdev = pds_vfio->vfio_coredev.pdev; |
| 146 |
struct device *pdsc_dev = &pci_physfn(pdev)->dev; |
| 147 |
struct pds_lm_sg_elem *sgl; |
| 148 |
dma_addr_t sgl_addr; |
| 149 |
size_t sgl_size; |
| 150 |
u32 max_sge; |
| 151 |
|
| 152 |
max_sge = DIV_ROUND_UP(page_count, PAGE_SIZE * 8); |
| 153 |
sgl_size = max_sge * sizeof(struct pds_lm_sg_elem); |
| 154 |
|
| 155 |
sgl = kzalloc(sgl_size, GFP_KERNEL); |
| 156 |
if (!sgl) |
| 157 |
return -ENOMEM; |
| 158 |
|
| 159 |
sgl_addr = dma_map_single(pdsc_dev, sgl, sgl_size, DMA_BIDIRECTIONAL); |
| 160 |
if (dma_mapping_error(pdsc_dev, sgl_addr)) { |
| 161 |
kfree(sgl); |
| 162 |
return -EIO; |
| 163 |
} |
| 164 |
|
| 165 |
region->sgl = sgl; |
| 166 |
region->num_sge = max_sge; |
| 167 |
region->sgl_addr = sgl_addr; |
| 168 |
|
| 169 |
return 0; |
| 170 |
} |
| 171 |
|
| 172 |
static void pds_vfio_dirty_free_regions(struct pds_vfio_dirty *dirty) |
| 173 |
{ |
| 174 |
vfree(dirty->regions); |
| 175 |
dirty->regions = NULL; |
| 176 |
dirty->num_regions = 0; |
| 177 |
} |
| 178 |
|
| 179 |
static int pds_vfio_dirty_alloc_regions(struct pds_vfio_pci_device *pds_vfio, |
| 180 |
struct pds_lm_dirty_region_info *region_info, |
| 181 |
u64 region_page_size, u8 num_regions) |
| 182 |
{ |
| 183 |
struct pci_dev *pdev = pds_vfio->vfio_coredev.pdev; |
| 184 |
struct pds_vfio_dirty *dirty = &pds_vfio->dirty; |
| 185 |
u32 dev_bmp_offset_byte = 0; |
| 186 |
int err; |
| 187 |
|
| 188 |
dirty->regions = vcalloc(num_regions, sizeof(struct pds_vfio_region)); |
| 189 |
if (!dirty->regions) |
| 190 |
return -ENOMEM; |
| 191 |
dirty->num_regions = num_regions; |
| 192 |
|
| 193 |
for (int i = 0; i < num_regions; i++) { |
| 194 |
struct pds_lm_dirty_region_info *ri = ®ion_info[i]; |
| 195 |
struct pds_vfio_region *region = &dirty->regions[i]; |
| 196 |
u64 region_size, region_start; |
| 197 |
u32 page_count; |
| 198 |
|
| 199 |
/* page_count might be adjusted by the device */ |
| 200 |
page_count = le32_to_cpu(ri->page_count); |
| 201 |
region_start = le64_to_cpu(ri->dma_base); |
| 202 |
region_size = page_count * region_page_size; |
| 203 |
|
| 204 |
err = pds_vfio_dirty_alloc_bitmaps(region, |
| 205 |
page_count / BITS_PER_BYTE); |
| 206 |
if (err) { |
| 207 |
dev_err(&pdev->dev, "Failed to alloc dirty bitmaps: %pe\n", |
| 208 |
ERR_PTR(err)); |
| 209 |
goto out_free_regions; |
| 210 |
} |
| 211 |
|
| 212 |
err = pds_vfio_dirty_alloc_sgl(pds_vfio, region, page_count); |
| 213 |
if (err) { |
| 214 |
dev_err(&pdev->dev, "Failed to alloc dirty sg lists: %pe\n", |
| 215 |
ERR_PTR(err)); |
| 216 |
goto out_free_regions; |
| 217 |
} |
| 218 |
|
| 219 |
region->size = region_size; |
| 220 |
region->start = region_start; |
| 221 |
region->page_size = region_page_size; |
| 222 |
region->dev_bmp_offset_start_byte = dev_bmp_offset_byte; |
| 223 |
|
| 224 |
dev_bmp_offset_byte += page_count / BITS_PER_BYTE; |
| 225 |
if (dev_bmp_offset_byte % BITS_PER_BYTE) { |
| 226 |
dev_err(&pdev->dev, "Device bitmap offset is mis-aligned\n"); |
| 227 |
err = -EINVAL; |
| 228 |
goto out_free_regions; |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
return 0; |
| 233 |
|
| 234 |
out_free_regions: |
| 235 |
pds_vfio_dirty_free_bitmaps(dirty); |
| 236 |
pds_vfio_dirty_free_sgl(pds_vfio); |
| 237 |
pds_vfio_dirty_free_regions(dirty); |
| 238 |
|
| 239 |
return err; |
| 240 |
} |
| 241 |
|
| 242 |
static int pds_vfio_dirty_enable(struct pds_vfio_pci_device *pds_vfio, |
| 243 |
struct rb_root_cached *ranges, u32 nnodes, |
| 244 |
u64 *page_size) |
| 245 |
{ |
| 246 |
struct pci_dev *pdev = pds_vfio->vfio_coredev.pdev; |
| 247 |
struct device *pdsc_dev = &pci_physfn(pdev)->dev; |
| 248 |
struct pds_lm_dirty_region_info *region_info; |
| 249 |
struct interval_tree_node *node = NULL; |
| 250 |
u64 region_page_size = *page_size; |
| 251 |
u8 max_regions = 0, num_regions; |
| 252 |
dma_addr_t regions_dma = 0; |
| 253 |
u32 num_ranges = nnodes; |
| 254 |
int err; |
| 255 |
u16 len; |
| 256 |
|
| 257 |
dev_dbg(&pdev->dev, "vf%u: Start dirty page tracking\n", |
| 258 |
pds_vfio->vf_id); |
| 259 |
|
| 260 |
if (pds_vfio_dirty_is_enabled(pds_vfio)) |
| 261 |
return -EINVAL; |
| 262 |
|
| 263 |
/* find if dirty tracking is disabled, i.e. num_regions == 0 */ |
| 264 |
err = pds_vfio_dirty_status_cmd(pds_vfio, 0, &max_regions, |
| 265 |
&num_regions); |
| 266 |
if (err < 0) { |
| 267 |
dev_err(&pdev->dev, "Failed to get dirty status, err %pe\n", |
| 268 |
ERR_PTR(err)); |
| 269 |
return err; |
| 270 |
} else if (num_regions) { |
| 271 |
dev_err(&pdev->dev, |
| 272 |
"Dirty tracking already enabled for %d regions\n", |
| 273 |
num_regions); |
| 274 |
return -EEXIST; |
| 275 |
} else if (!max_regions) { |
| 276 |
dev_err(&pdev->dev, |
| 277 |
"Device doesn't support dirty tracking, max_regions %d\n", |
| 278 |
max_regions); |
| 279 |
return -EOPNOTSUPP; |
| 280 |
} |
| 281 |
|
| 282 |
if (num_ranges > max_regions) { |
| 283 |
vfio_combine_iova_ranges(ranges, nnodes, max_regions); |
| 284 |
num_ranges = max_regions; |
| 285 |
} |
| 286 |
|
| 287 |
region_info = kzalloc_objs(*region_info, num_ranges); |
| 288 |
if (!region_info) |
| 289 |
return -ENOMEM; |
| 290 |
len = num_ranges * sizeof(*region_info); |
| 291 |
|
| 292 |
node = interval_tree_iter_first(ranges, 0, ULONG_MAX); |
| 293 |
if (!node) { |
| 294 |
err = -EINVAL; |
| 295 |
goto out_free_region_info; |
| 296 |
} |
| 297 |
|
| 298 |
for (int i = 0; i < num_ranges; i++) { |
| 299 |
struct pds_lm_dirty_region_info *ri = ®ion_info[i]; |
| 300 |
u64 region_size = node->last - node->start + 1; |
| 301 |
u64 region_start = node->start; |
| 302 |
u32 page_count; |
| 303 |
|
| 304 |
page_count = DIV_ROUND_UP(region_size, region_page_size); |
| 305 |
|
| 306 |
ri->dma_base = cpu_to_le64(region_start); |
| 307 |
ri->page_count = cpu_to_le32(page_count); |
| 308 |
ri->page_size_log2 = ilog2(region_page_size); |
| 309 |
|
| 310 |
dev_dbg(&pdev->dev, |
| 311 |
"region_info[%d]: region_start 0x%llx region_end 0x%lx region_size 0x%llx page_count %u page_size %llu\n", |
| 312 |
i, region_start, node->last, region_size, page_count, |
| 313 |
region_page_size); |
| 314 |
|
| 315 |
node = interval_tree_iter_next(node, 0, ULONG_MAX); |
| 316 |
} |
| 317 |
|
| 318 |
regions_dma = dma_map_single(pdsc_dev, (void *)region_info, len, |
| 319 |
DMA_BIDIRECTIONAL); |
| 320 |
if (dma_mapping_error(pdsc_dev, regions_dma)) { |
| 321 |
err = -ENOMEM; |
| 322 |
goto out_free_region_info; |
| 323 |
} |
| 324 |
|
| 325 |
err = pds_vfio_dirty_enable_cmd(pds_vfio, regions_dma, num_ranges); |
| 326 |
dma_unmap_single(pdsc_dev, regions_dma, len, DMA_BIDIRECTIONAL); |
| 327 |
if (err) |
| 328 |
goto out_free_region_info; |
| 329 |
|
| 330 |
err = pds_vfio_dirty_alloc_regions(pds_vfio, region_info, |
| 331 |
region_page_size, num_ranges); |
| 332 |
if (err) { |
| 333 |
dev_err(&pdev->dev, |
| 334 |
"Failed to allocate %d regions for tracking dirty regions: %pe\n", |
| 335 |
num_regions, ERR_PTR(err)); |
| 336 |
goto out_dirty_disable; |
| 337 |
} |
| 338 |
|
| 339 |
pds_vfio_dirty_set_enabled(pds_vfio); |
| 340 |
|
| 341 |
pds_vfio_print_guest_region_info(pds_vfio, max_regions); |
| 342 |
|
| 343 |
kfree(region_info); |
| 344 |
|
| 345 |
return 0; |
| 346 |
|
| 347 |
out_dirty_disable: |
| 348 |
pds_vfio_dirty_disable_cmd(pds_vfio); |
| 349 |
out_free_region_info: |
| 350 |
kfree(region_info); |
| 351 |
return err; |
| 352 |
} |
| 353 |
|
| 354 |
void pds_vfio_dirty_disable(struct pds_vfio_pci_device *pds_vfio, bool send_cmd) |
| 355 |
{ |
| 356 |
if (pds_vfio_dirty_is_enabled(pds_vfio)) { |
| 357 |
pds_vfio_dirty_set_disabled(pds_vfio); |
| 358 |
if (send_cmd) |
| 359 |
pds_vfio_dirty_disable_cmd(pds_vfio); |
| 360 |
pds_vfio_dirty_free_sgl(pds_vfio); |
| 361 |
pds_vfio_dirty_free_bitmaps(&pds_vfio->dirty); |
| 362 |
pds_vfio_dirty_free_regions(&pds_vfio->dirty); |
| 363 |
} |
| 364 |
|
| 365 |
if (send_cmd) |
| 366 |
pds_vfio_send_host_vf_lm_status_cmd(pds_vfio, PDS_LM_STA_NONE); |
| 367 |
} |
| 368 |
|
| 369 |
static int pds_vfio_dirty_seq_ack(struct pds_vfio_pci_device *pds_vfio, |
| 370 |
struct pds_vfio_region *region, |
| 371 |
unsigned long *seq_ack_bmp, u32 offset, |
| 372 |
u32 bmp_bytes, bool read_seq) |
| 373 |
{ |
| 374 |
const char *bmp_type_str = read_seq ? "read_seq" : "write_ack"; |
| 375 |
u8 dma_dir = read_seq ? DMA_FROM_DEVICE : DMA_TO_DEVICE; |
| 376 |
struct pci_dev *pdev = pds_vfio->vfio_coredev.pdev; |
| 377 |
struct device *pdsc_dev = &pci_physfn(pdev)->dev; |
| 378 |
unsigned long long npages; |
| 379 |
struct sg_table sg_table; |
| 380 |
struct scatterlist *sg; |
| 381 |
struct page **pages; |
| 382 |
u32 page_offset; |
| 383 |
const void *bmp; |
| 384 |
size_t size; |
| 385 |
u16 num_sge; |
| 386 |
int err; |
| 387 |
int i; |
| 388 |
|
| 389 |
bmp = (void *)((u64)seq_ack_bmp + offset); |
| 390 |
page_offset = offset_in_page(bmp); |
| 391 |
bmp -= page_offset; |
| 392 |
|
| 393 |
/* |
| 394 |
* Start and end of bitmap section to seq/ack might not be page |
| 395 |
* aligned, so use the page_offset to account for that so there |
| 396 |
* will be enough pages to represent the bmp_bytes |
| 397 |
*/ |
| 398 |
npages = DIV_ROUND_UP_ULL(bmp_bytes + page_offset, PAGE_SIZE); |
| 399 |
pages = kmalloc_objs(*pages, npages); |
| 400 |
if (!pages) |
| 401 |
return -ENOMEM; |
| 402 |
|
| 403 |
for (unsigned long long i = 0; i < npages; i++) { |
| 404 |
struct page *page = vmalloc_to_page(bmp); |
| 405 |
|
| 406 |
if (!page) { |
| 407 |
err = -EFAULT; |
| 408 |
goto out_free_pages; |
| 409 |
} |
| 410 |
|
| 411 |
pages[i] = page; |
| 412 |
bmp += PAGE_SIZE; |
| 413 |
} |
| 414 |
|
| 415 |
err = sg_alloc_table_from_pages(&sg_table, pages, npages, page_offset, |
| 416 |
bmp_bytes, GFP_KERNEL); |
| 417 |
if (err) |
| 418 |
goto out_free_pages; |
| 419 |
|
| 420 |
err = dma_map_sgtable(pdsc_dev, &sg_table, dma_dir, 0); |
| 421 |
if (err) |
| 422 |
goto out_free_sg_table; |
| 423 |
|
| 424 |
for_each_sgtable_dma_sg(&sg_table, sg, i) { |
| 425 |
struct pds_lm_sg_elem *sg_elem = ®ion->sgl[i]; |
| 426 |
|
| 427 |
sg_elem->addr = cpu_to_le64(sg_dma_address(sg)); |
| 428 |
sg_elem->len = cpu_to_le32(sg_dma_len(sg)); |
| 429 |
} |
| 430 |
|
| 431 |
num_sge = sg_table.nents; |
| 432 |
size = num_sge * sizeof(struct pds_lm_sg_elem); |
| 433 |
offset += region->dev_bmp_offset_start_byte; |
| 434 |
dma_sync_single_for_device(pdsc_dev, region->sgl_addr, size, dma_dir); |
| 435 |
err = pds_vfio_dirty_seq_ack_cmd(pds_vfio, region->sgl_addr, num_sge, |
| 436 |
offset, bmp_bytes, read_seq); |
| 437 |
if (err) |
| 438 |
dev_err(&pdev->dev, |
| 439 |
"Dirty bitmap %s failed offset %u bmp_bytes %u num_sge %u DMA 0x%llx: %pe\n", |
| 440 |
bmp_type_str, offset, bmp_bytes, |
| 441 |
num_sge, region->sgl_addr, ERR_PTR(err)); |
| 442 |
dma_sync_single_for_cpu(pdsc_dev, region->sgl_addr, size, dma_dir); |
| 443 |
|
| 444 |
dma_unmap_sgtable(pdsc_dev, &sg_table, dma_dir, 0); |
| 445 |
out_free_sg_table: |
| 446 |
sg_free_table(&sg_table); |
| 447 |
out_free_pages: |
| 448 |
kfree(pages); |
| 449 |
|
| 450 |
return err; |
| 451 |
} |
| 452 |
|
| 453 |
static int pds_vfio_dirty_write_ack(struct pds_vfio_pci_device *pds_vfio, |
| 454 |
struct pds_vfio_region *region, |
| 455 |
u32 offset, u32 len) |
| 456 |
{ |
| 457 |
|
| 458 |
return pds_vfio_dirty_seq_ack(pds_vfio, region, region->host_ack, |
| 459 |
offset, len, WRITE_ACK); |
| 460 |
} |
| 461 |
|
| 462 |
static int pds_vfio_dirty_read_seq(struct pds_vfio_pci_device *pds_vfio, |
| 463 |
struct pds_vfio_region *region, |
| 464 |
u32 offset, u32 len) |
| 465 |
{ |
| 466 |
return pds_vfio_dirty_seq_ack(pds_vfio, region, region->host_seq, |
| 467 |
offset, len, READ_SEQ); |
| 468 |
} |
| 469 |
|
| 470 |
static int pds_vfio_dirty_process_bitmaps(struct pds_vfio_pci_device *pds_vfio, |
| 471 |
struct pds_vfio_region *region, |
| 472 |
struct iova_bitmap *dirty_bitmap, |
| 473 |
u32 bmp_offset, u32 len_bytes) |
| 474 |
{ |
| 475 |
u64 page_size = region->page_size; |
| 476 |
u64 region_start = region->start; |
| 477 |
u32 bmp_offset_bit; |
| 478 |
__le64 *seq, *ack; |
| 479 |
int dword_count; |
| 480 |
|
| 481 |
dword_count = len_bytes / sizeof(u64); |
| 482 |
seq = (__le64 *)((u64)region->host_seq + bmp_offset); |
| 483 |
ack = (__le64 *)((u64)region->host_ack + bmp_offset); |
| 484 |
bmp_offset_bit = bmp_offset * 8; |
| 485 |
|
| 486 |
for (int i = 0; i < dword_count; i++) { |
| 487 |
u64 xor = le64_to_cpu(seq[i]) ^ le64_to_cpu(ack[i]); |
| 488 |
|
| 489 |
/* prepare for next write_ack call */ |
| 490 |
ack[i] = seq[i]; |
| 491 |
|
| 492 |
for (u8 bit_i = 0; bit_i < BITS_PER_TYPE(u64); ++bit_i) { |
| 493 |
if (xor & BIT(bit_i)) { |
| 494 |
u64 abs_bit_i = bmp_offset_bit + |
| 495 |
i * BITS_PER_TYPE(u64) + bit_i; |
| 496 |
u64 addr = abs_bit_i * page_size + region_start; |
| 497 |
|
| 498 |
iova_bitmap_set(dirty_bitmap, addr, page_size); |
| 499 |
} |
| 500 |
} |
| 501 |
} |
| 502 |
|
| 503 |
return 0; |
| 504 |
} |
| 505 |
|
| 506 |
static struct pds_vfio_region * |
| 507 |
pds_vfio_get_region(struct pds_vfio_pci_device *pds_vfio, unsigned long iova) |
| 508 |
{ |
| 509 |
struct pds_vfio_dirty *dirty = &pds_vfio->dirty; |
| 510 |
|
| 511 |
for (int i = 0; i < dirty->num_regions; i++) { |
| 512 |
struct pds_vfio_region *region = &dirty->regions[i]; |
| 513 |
|
| 514 |
if (iova >= region->start && |
| 515 |
iova < (region->start + region->size)) |
| 516 |
return region; |
| 517 |
} |
| 518 |
|
| 519 |
return NULL; |
| 520 |
} |
| 521 |
|
| 522 |
static int pds_vfio_dirty_sync(struct pds_vfio_pci_device *pds_vfio, |
| 523 |
struct iova_bitmap *dirty_bitmap, |
| 524 |
unsigned long iova, unsigned long length) |
| 525 |
{ |
| 526 |
struct device *dev = &pds_vfio->vfio_coredev.pdev->dev; |
| 527 |
struct pds_vfio_region *region; |
| 528 |
u64 bmp_offset, bmp_bytes; |
| 529 |
u64 bitmap_size, pages; |
| 530 |
int err; |
| 531 |
|
| 532 |
dev_dbg(dev, "vf%u: Get dirty page bitmap\n", pds_vfio->vf_id); |
| 533 |
|
| 534 |
if (!pds_vfio_dirty_is_enabled(pds_vfio)) { |
| 535 |
dev_err(dev, "vf%u: Sync failed, dirty tracking is disabled\n", |
| 536 |
pds_vfio->vf_id); |
| 537 |
return -EINVAL; |
| 538 |
} |
| 539 |
|
| 540 |
region = pds_vfio_get_region(pds_vfio, iova); |
| 541 |
if (!region) { |
| 542 |
dev_err(dev, "vf%u: Failed to find region that contains iova 0x%lx length 0x%lx\n", |
| 543 |
pds_vfio->vf_id, iova, length); |
| 544 |
return -EINVAL; |
| 545 |
} |
| 546 |
|
| 547 |
pages = DIV_ROUND_UP(length, region->page_size); |
| 548 |
bitmap_size = |
| 549 |
round_up(pages, sizeof(u64) * BITS_PER_BYTE) / BITS_PER_BYTE; |
| 550 |
|
| 551 |
dev_dbg(dev, |
| 552 |
"vf%u: iova 0x%lx length %lu page_size %llu pages %llu bitmap_size %llu\n", |
| 553 |
pds_vfio->vf_id, iova, length, region->page_size, |
| 554 |
pages, bitmap_size); |
| 555 |
|
| 556 |
if (!length || ((iova - region->start + length) > region->size)) { |
| 557 |
dev_err(dev, "Invalid iova 0x%lx and/or length 0x%lx to sync\n", |
| 558 |
iova, length); |
| 559 |
return -EINVAL; |
| 560 |
} |
| 561 |
|
| 562 |
/* bitmap is modified in 64 bit chunks */ |
| 563 |
bmp_bytes = ALIGN(DIV_ROUND_UP(length / region->page_size, |
| 564 |
sizeof(u64)), sizeof(u64)); |
| 565 |
if (bmp_bytes != bitmap_size) { |
| 566 |
dev_err(dev, |
| 567 |
"Calculated bitmap bytes %llu not equal to bitmap size %llu\n", |
| 568 |
bmp_bytes, bitmap_size); |
| 569 |
return -EINVAL; |
| 570 |
} |
| 571 |
|
| 572 |
if (bmp_bytes > region->bmp_bytes) { |
| 573 |
dev_err(dev, |
| 574 |
"Calculated bitmap bytes %llu larger than region's cached bmp_bytes %llu\n", |
| 575 |
bmp_bytes, region->bmp_bytes); |
| 576 |
return -EINVAL; |
| 577 |
} |
| 578 |
|
| 579 |
bmp_offset = DIV_ROUND_UP((iova - region->start) / |
| 580 |
region->page_size, sizeof(u64)); |
| 581 |
|
| 582 |
dev_dbg(dev, |
| 583 |
"Syncing dirty bitmap, iova 0x%lx length 0x%lx, bmp_offset %llu bmp_bytes %llu\n", |
| 584 |
iova, length, bmp_offset, bmp_bytes); |
| 585 |
|
| 586 |
err = pds_vfio_dirty_read_seq(pds_vfio, region, bmp_offset, bmp_bytes); |
| 587 |
if (err) |
| 588 |
return err; |
| 589 |
|
| 590 |
err = pds_vfio_dirty_process_bitmaps(pds_vfio, region, dirty_bitmap, |
| 591 |
bmp_offset, bmp_bytes); |
| 592 |
if (err) |
| 593 |
return err; |
| 594 |
|
| 595 |
err = pds_vfio_dirty_write_ack(pds_vfio, region, bmp_offset, bmp_bytes); |
| 596 |
if (err) |
| 597 |
return err; |
| 598 |
|
| 599 |
return 0; |
| 600 |
} |
| 601 |
|
| 602 |
int pds_vfio_dma_logging_report(struct vfio_device *vdev, unsigned long iova, |
| 603 |
unsigned long length, struct iova_bitmap *dirty) |
| 604 |
{ |
| 605 |
struct pds_vfio_pci_device *pds_vfio = |
| 606 |
container_of(vdev, struct pds_vfio_pci_device, |
| 607 |
vfio_coredev.vdev); |
| 608 |
int err; |
| 609 |
|
| 610 |
mutex_lock(&pds_vfio->state_mutex); |
| 611 |
err = pds_vfio_dirty_sync(pds_vfio, dirty, iova, length); |
| 612 |
mutex_unlock(&pds_vfio->state_mutex); |
| 613 |
|
| 614 |
return err; |
| 615 |
} |
| 616 |
|
| 617 |
int pds_vfio_dma_logging_start(struct vfio_device *vdev, |
| 618 |
struct rb_root_cached *ranges, u32 nnodes, |
| 619 |
u64 *page_size) |
| 620 |
{ |
| 621 |
struct pds_vfio_pci_device *pds_vfio = |
| 622 |
container_of(vdev, struct pds_vfio_pci_device, |
| 623 |
vfio_coredev.vdev); |
| 624 |
int err; |
| 625 |
|
| 626 |
mutex_lock(&pds_vfio->state_mutex); |
| 627 |
pds_vfio_send_host_vf_lm_status_cmd(pds_vfio, PDS_LM_STA_IN_PROGRESS); |
| 628 |
err = pds_vfio_dirty_enable(pds_vfio, ranges, nnodes, page_size); |
| 629 |
mutex_unlock(&pds_vfio->state_mutex); |
| 630 |
|
| 631 |
return err; |
| 632 |
} |
| 633 |
|
| 634 |
int pds_vfio_dma_logging_stop(struct vfio_device *vdev) |
| 635 |
{ |
| 636 |
struct pds_vfio_pci_device *pds_vfio = |
| 637 |
container_of(vdev, struct pds_vfio_pci_device, |
| 638 |
vfio_coredev.vdev); |
| 639 |
|
| 640 |
mutex_lock(&pds_vfio->state_mutex); |
| 641 |
pds_vfio_dirty_disable(pds_vfio, true); |
| 642 |
mutex_unlock(&pds_vfio->state_mutex); |
| 643 |
|
| 644 |
return 0; |
| 645 |
} |