diff options
Diffstat (limited to 'arch/x86/kernel/amd_iommu.c')
-rw-r--r-- | arch/x86/kernel/amd_iommu.c | 519 |
1 files changed, 302 insertions, 217 deletions
diff --git a/arch/x86/kernel/amd_iommu.c b/arch/x86/kernel/amd_iommu.c index 57ca7778722..dc5dddafe5c 100644 --- a/arch/x86/kernel/amd_iommu.c +++ b/arch/x86/kernel/amd_iommu.c | |||
@@ -18,6 +18,7 @@ | |||
18 | */ | 18 | */ |
19 | 19 | ||
20 | #include <linux/pci.h> | 20 | #include <linux/pci.h> |
21 | #include <linux/pci-ats.h> | ||
21 | #include <linux/bitmap.h> | 22 | #include <linux/bitmap.h> |
22 | #include <linux/slab.h> | 23 | #include <linux/slab.h> |
23 | #include <linux/debugfs.h> | 24 | #include <linux/debugfs.h> |
@@ -25,6 +26,7 @@ | |||
25 | #include <linux/dma-mapping.h> | 26 | #include <linux/dma-mapping.h> |
26 | #include <linux/iommu-helper.h> | 27 | #include <linux/iommu-helper.h> |
27 | #include <linux/iommu.h> | 28 | #include <linux/iommu.h> |
29 | #include <linux/delay.h> | ||
28 | #include <asm/proto.h> | 30 | #include <asm/proto.h> |
29 | #include <asm/iommu.h> | 31 | #include <asm/iommu.h> |
30 | #include <asm/gart.h> | 32 | #include <asm/gart.h> |
@@ -34,7 +36,7 @@ | |||
34 | 36 | ||
35 | #define CMD_SET_TYPE(cmd, t) ((cmd)->data[1] |= ((t) << 28)) | 37 | #define CMD_SET_TYPE(cmd, t) ((cmd)->data[1] |= ((t) << 28)) |
36 | 38 | ||
37 | #define EXIT_LOOP_COUNT 10000000 | 39 | #define LOOP_TIMEOUT 100000 |
38 | 40 | ||
39 | static DEFINE_RWLOCK(amd_iommu_devtable_lock); | 41 | static DEFINE_RWLOCK(amd_iommu_devtable_lock); |
40 | 42 | ||
@@ -57,7 +59,6 @@ struct iommu_cmd { | |||
57 | u32 data[4]; | 59 | u32 data[4]; |
58 | }; | 60 | }; |
59 | 61 | ||
60 | static void reset_iommu_command_buffer(struct amd_iommu *iommu); | ||
61 | static void update_domain(struct protection_domain *domain); | 62 | static void update_domain(struct protection_domain *domain); |
62 | 63 | ||
63 | /**************************************************************************** | 64 | /**************************************************************************** |
@@ -322,8 +323,6 @@ static void iommu_print_event(struct amd_iommu *iommu, void *__evt) | |||
322 | break; | 323 | break; |
323 | case EVENT_TYPE_ILL_CMD: | 324 | case EVENT_TYPE_ILL_CMD: |
324 | printk("ILLEGAL_COMMAND_ERROR address=0x%016llx]\n", address); | 325 | printk("ILLEGAL_COMMAND_ERROR address=0x%016llx]\n", address); |
325 | iommu->reset_in_progress = true; | ||
326 | reset_iommu_command_buffer(iommu); | ||
327 | dump_command(address); | 326 | dump_command(address); |
328 | break; | 327 | break; |
329 | case EVENT_TYPE_CMD_HARD_ERR: | 328 | case EVENT_TYPE_CMD_HARD_ERR: |
@@ -383,186 +382,289 @@ irqreturn_t amd_iommu_int_handler(int irq, void *data) | |||
383 | * | 382 | * |
384 | ****************************************************************************/ | 383 | ****************************************************************************/ |
385 | 384 | ||
386 | /* | 385 | static int wait_on_sem(volatile u64 *sem) |
387 | * Writes the command to the IOMMUs command buffer and informs the | 386 | { |
388 | * hardware about the new command. Must be called with iommu->lock held. | 387 | int i = 0; |
389 | */ | 388 | |
390 | static int __iommu_queue_command(struct amd_iommu *iommu, struct iommu_cmd *cmd) | 389 | while (*sem == 0 && i < LOOP_TIMEOUT) { |
390 | udelay(1); | ||
391 | i += 1; | ||
392 | } | ||
393 | |||
394 | if (i == LOOP_TIMEOUT) { | ||
395 | pr_alert("AMD-Vi: Completion-Wait loop timed out\n"); | ||
396 | return -EIO; | ||
397 | } | ||
398 | |||
399 | return 0; | ||
400 | } | ||
401 | |||
402 | static void copy_cmd_to_buffer(struct amd_iommu *iommu, | ||
403 | struct iommu_cmd *cmd, | ||
404 | u32 tail) | ||
391 | { | 405 | { |
392 | u32 tail, head; | ||
393 | u8 *target; | 406 | u8 *target; |
394 | 407 | ||
395 | WARN_ON(iommu->cmd_buf_size & CMD_BUFFER_UNINITIALIZED); | ||
396 | tail = readl(iommu->mmio_base + MMIO_CMD_TAIL_OFFSET); | ||
397 | target = iommu->cmd_buf + tail; | 408 | target = iommu->cmd_buf + tail; |
398 | memcpy_toio(target, cmd, sizeof(*cmd)); | 409 | tail = (tail + sizeof(*cmd)) % iommu->cmd_buf_size; |
399 | tail = (tail + sizeof(*cmd)) % iommu->cmd_buf_size; | 410 | |
400 | head = readl(iommu->mmio_base + MMIO_CMD_HEAD_OFFSET); | 411 | /* Copy command to buffer */ |
401 | if (tail == head) | 412 | memcpy(target, cmd, sizeof(*cmd)); |
402 | return -ENOMEM; | 413 | |
414 | /* Tell the IOMMU about it */ | ||
403 | writel(tail, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET); | 415 | writel(tail, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET); |
416 | } | ||
404 | 417 | ||
405 | return 0; | 418 | static void build_completion_wait(struct iommu_cmd *cmd, u64 address) |
419 | { | ||
420 | WARN_ON(address & 0x7ULL); | ||
421 | |||
422 | memset(cmd, 0, sizeof(*cmd)); | ||
423 | cmd->data[0] = lower_32_bits(__pa(address)) | CMD_COMPL_WAIT_STORE_MASK; | ||
424 | cmd->data[1] = upper_32_bits(__pa(address)); | ||
425 | cmd->data[2] = 1; | ||
426 | CMD_SET_TYPE(cmd, CMD_COMPL_WAIT); | ||
427 | } | ||
428 | |||
429 | static void build_inv_dte(struct iommu_cmd *cmd, u16 devid) | ||
430 | { | ||
431 | memset(cmd, 0, sizeof(*cmd)); | ||
432 | cmd->data[0] = devid; | ||
433 | CMD_SET_TYPE(cmd, CMD_INV_DEV_ENTRY); | ||
434 | } | ||
435 | |||
436 | static void build_inv_iommu_pages(struct iommu_cmd *cmd, u64 address, | ||
437 | size_t size, u16 domid, int pde) | ||
438 | { | ||
439 | u64 pages; | ||
440 | int s; | ||
441 | |||
442 | pages = iommu_num_pages(address, size, PAGE_SIZE); | ||
443 | s = 0; | ||
444 | |||
445 | if (pages > 1) { | ||
446 | /* | ||
447 | * If we have to flush more than one page, flush all | ||
448 | * TLB entries for this domain | ||
449 | */ | ||
450 | address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS; | ||
451 | s = 1; | ||
452 | } | ||
453 | |||
454 | address &= PAGE_MASK; | ||
455 | |||
456 | memset(cmd, 0, sizeof(*cmd)); | ||
457 | cmd->data[1] |= domid; | ||
458 | cmd->data[2] = lower_32_bits(address); | ||
459 | cmd->data[3] = upper_32_bits(address); | ||
460 | CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES); | ||
461 | if (s) /* size bit - we flush more than one 4kb page */ | ||
462 | cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK; | ||
463 | if (pde) /* PDE bit - we wan't flush everything not only the PTEs */ | ||
464 | cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK; | ||
465 | } | ||
466 | |||
467 | static void build_inv_iotlb_pages(struct iommu_cmd *cmd, u16 devid, int qdep, | ||
468 | u64 address, size_t size) | ||
469 | { | ||
470 | u64 pages; | ||
471 | int s; | ||
472 | |||
473 | pages = iommu_num_pages(address, size, PAGE_SIZE); | ||
474 | s = 0; | ||
475 | |||
476 | if (pages > 1) { | ||
477 | /* | ||
478 | * If we have to flush more than one page, flush all | ||
479 | * TLB entries for this domain | ||
480 | */ | ||
481 | address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS; | ||
482 | s = 1; | ||
483 | } | ||
484 | |||
485 | address &= PAGE_MASK; | ||
486 | |||
487 | memset(cmd, 0, sizeof(*cmd)); | ||
488 | cmd->data[0] = devid; | ||
489 | cmd->data[0] |= (qdep & 0xff) << 24; | ||
490 | cmd->data[1] = devid; | ||
491 | cmd->data[2] = lower_32_bits(address); | ||
492 | cmd->data[3] = upper_32_bits(address); | ||
493 | CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES); | ||
494 | if (s) | ||
495 | cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK; | ||
496 | } | ||
497 | |||
498 | static void build_inv_all(struct iommu_cmd *cmd) | ||
499 | { | ||
500 | memset(cmd, 0, sizeof(*cmd)); | ||
501 | CMD_SET_TYPE(cmd, CMD_INV_ALL); | ||
406 | } | 502 | } |
407 | 503 | ||
408 | /* | 504 | /* |
409 | * General queuing function for commands. Takes iommu->lock and calls | 505 | * Writes the command to the IOMMUs command buffer and informs the |
410 | * __iommu_queue_command(). | 506 | * hardware about the new command. |
411 | */ | 507 | */ |
412 | static int iommu_queue_command(struct amd_iommu *iommu, struct iommu_cmd *cmd) | 508 | static int iommu_queue_command(struct amd_iommu *iommu, struct iommu_cmd *cmd) |
413 | { | 509 | { |
510 | u32 left, tail, head, next_tail; | ||
414 | unsigned long flags; | 511 | unsigned long flags; |
415 | int ret; | ||
416 | 512 | ||
513 | WARN_ON(iommu->cmd_buf_size & CMD_BUFFER_UNINITIALIZED); | ||
514 | |||
515 | again: | ||
417 | spin_lock_irqsave(&iommu->lock, flags); | 516 | spin_lock_irqsave(&iommu->lock, flags); |
418 | ret = __iommu_queue_command(iommu, cmd); | ||
419 | if (!ret) | ||
420 | iommu->need_sync = true; | ||
421 | spin_unlock_irqrestore(&iommu->lock, flags); | ||
422 | 517 | ||
423 | return ret; | 518 | head = readl(iommu->mmio_base + MMIO_CMD_HEAD_OFFSET); |
424 | } | 519 | tail = readl(iommu->mmio_base + MMIO_CMD_TAIL_OFFSET); |
520 | next_tail = (tail + sizeof(*cmd)) % iommu->cmd_buf_size; | ||
521 | left = (head - next_tail) % iommu->cmd_buf_size; | ||
425 | 522 | ||
426 | /* | 523 | if (left <= 2) { |
427 | * This function waits until an IOMMU has completed a completion | 524 | struct iommu_cmd sync_cmd; |
428 | * wait command | 525 | volatile u64 sem = 0; |
429 | */ | 526 | int ret; |
430 | static void __iommu_wait_for_completion(struct amd_iommu *iommu) | ||
431 | { | ||
432 | int ready = 0; | ||
433 | unsigned status = 0; | ||
434 | unsigned long i = 0; | ||
435 | 527 | ||
436 | INC_STATS_COUNTER(compl_wait); | 528 | build_completion_wait(&sync_cmd, (u64)&sem); |
529 | copy_cmd_to_buffer(iommu, &sync_cmd, tail); | ||
437 | 530 | ||
438 | while (!ready && (i < EXIT_LOOP_COUNT)) { | 531 | spin_unlock_irqrestore(&iommu->lock, flags); |
439 | ++i; | 532 | |
440 | /* wait for the bit to become one */ | 533 | if ((ret = wait_on_sem(&sem)) != 0) |
441 | status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET); | 534 | return ret; |
442 | ready = status & MMIO_STATUS_COM_WAIT_INT_MASK; | 535 | |
536 | goto again; | ||
443 | } | 537 | } |
444 | 538 | ||
445 | /* set bit back to zero */ | 539 | copy_cmd_to_buffer(iommu, cmd, tail); |
446 | status &= ~MMIO_STATUS_COM_WAIT_INT_MASK; | 540 | |
447 | writel(status, iommu->mmio_base + MMIO_STATUS_OFFSET); | 541 | /* We need to sync now to make sure all commands are processed */ |
542 | iommu->need_sync = true; | ||
543 | |||
544 | spin_unlock_irqrestore(&iommu->lock, flags); | ||
448 | 545 | ||
449 | if (unlikely(i == EXIT_LOOP_COUNT)) | 546 | return 0; |
450 | iommu->reset_in_progress = true; | ||
451 | } | 547 | } |
452 | 548 | ||
453 | /* | 549 | /* |
454 | * This function queues a completion wait command into the command | 550 | * This function queues a completion wait command into the command |
455 | * buffer of an IOMMU | 551 | * buffer of an IOMMU |
456 | */ | 552 | */ |
457 | static int __iommu_completion_wait(struct amd_iommu *iommu) | 553 | static int iommu_completion_wait(struct amd_iommu *iommu) |
458 | { | 554 | { |
459 | struct iommu_cmd cmd; | 555 | struct iommu_cmd cmd; |
556 | volatile u64 sem = 0; | ||
557 | int ret; | ||
460 | 558 | ||
461 | memset(&cmd, 0, sizeof(cmd)); | 559 | if (!iommu->need_sync) |
462 | cmd.data[0] = CMD_COMPL_WAIT_INT_MASK; | 560 | return 0; |
463 | CMD_SET_TYPE(&cmd, CMD_COMPL_WAIT); | 561 | |
562 | build_completion_wait(&cmd, (u64)&sem); | ||
563 | |||
564 | ret = iommu_queue_command(iommu, &cmd); | ||
565 | if (ret) | ||
566 | return ret; | ||
464 | 567 | ||
465 | return __iommu_queue_command(iommu, &cmd); | 568 | return wait_on_sem(&sem); |
466 | } | 569 | } |
467 | 570 | ||
468 | /* | 571 | static int iommu_flush_dte(struct amd_iommu *iommu, u16 devid) |
469 | * This function is called whenever we need to ensure that the IOMMU has | ||
470 | * completed execution of all commands we sent. It sends a | ||
471 | * COMPLETION_WAIT command and waits for it to finish. The IOMMU informs | ||
472 | * us about that by writing a value to a physical address we pass with | ||
473 | * the command. | ||
474 | */ | ||
475 | static int iommu_completion_wait(struct amd_iommu *iommu) | ||
476 | { | 572 | { |
477 | int ret = 0; | 573 | struct iommu_cmd cmd; |
478 | unsigned long flags; | ||
479 | 574 | ||
480 | spin_lock_irqsave(&iommu->lock, flags); | 575 | build_inv_dte(&cmd, devid); |
481 | 576 | ||
482 | if (!iommu->need_sync) | 577 | return iommu_queue_command(iommu, &cmd); |
483 | goto out; | 578 | } |
484 | 579 | ||
485 | ret = __iommu_completion_wait(iommu); | 580 | static void iommu_flush_dte_all(struct amd_iommu *iommu) |
581 | { | ||
582 | u32 devid; | ||
486 | 583 | ||
487 | iommu->need_sync = false; | 584 | for (devid = 0; devid <= 0xffff; ++devid) |
585 | iommu_flush_dte(iommu, devid); | ||
488 | 586 | ||
489 | if (ret) | 587 | iommu_completion_wait(iommu); |
490 | goto out; | 588 | } |
491 | |||
492 | __iommu_wait_for_completion(iommu); | ||
493 | 589 | ||
494 | out: | 590 | /* |
495 | spin_unlock_irqrestore(&iommu->lock, flags); | 591 | * This function uses heavy locking and may disable irqs for some time. But |
592 | * this is no issue because it is only called during resume. | ||
593 | */ | ||
594 | static void iommu_flush_tlb_all(struct amd_iommu *iommu) | ||
595 | { | ||
596 | u32 dom_id; | ||
496 | 597 | ||
497 | if (iommu->reset_in_progress) | 598 | for (dom_id = 0; dom_id <= 0xffff; ++dom_id) { |
498 | reset_iommu_command_buffer(iommu); | 599 | struct iommu_cmd cmd; |
600 | build_inv_iommu_pages(&cmd, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, | ||
601 | dom_id, 1); | ||
602 | iommu_queue_command(iommu, &cmd); | ||
603 | } | ||
499 | 604 | ||
500 | return 0; | 605 | iommu_completion_wait(iommu); |
501 | } | 606 | } |
502 | 607 | ||
503 | static void iommu_flush_complete(struct protection_domain *domain) | 608 | static void iommu_flush_all(struct amd_iommu *iommu) |
504 | { | 609 | { |
505 | int i; | 610 | struct iommu_cmd cmd; |
506 | 611 | ||
507 | for (i = 0; i < amd_iommus_present; ++i) { | 612 | build_inv_all(&cmd); |
508 | if (!domain->dev_iommu[i]) | ||
509 | continue; | ||
510 | 613 | ||
511 | /* | 614 | iommu_queue_command(iommu, &cmd); |
512 | * Devices of this domain are behind this IOMMU | 615 | iommu_completion_wait(iommu); |
513 | * We need to wait for completion of all commands. | 616 | } |
514 | */ | 617 | |
515 | iommu_completion_wait(amd_iommus[i]); | 618 | void iommu_flush_all_caches(struct amd_iommu *iommu) |
619 | { | ||
620 | if (iommu_feature(iommu, FEATURE_IA)) { | ||
621 | iommu_flush_all(iommu); | ||
622 | } else { | ||
623 | iommu_flush_dte_all(iommu); | ||
624 | iommu_flush_tlb_all(iommu); | ||
516 | } | 625 | } |
517 | } | 626 | } |
518 | 627 | ||
519 | /* | 628 | /* |
520 | * Command send function for invalidating a device table entry | 629 | * Command send function for flushing on-device TLB |
521 | */ | 630 | */ |
522 | static int iommu_flush_device(struct device *dev) | 631 | static int device_flush_iotlb(struct device *dev, u64 address, size_t size) |
523 | { | 632 | { |
633 | struct pci_dev *pdev = to_pci_dev(dev); | ||
524 | struct amd_iommu *iommu; | 634 | struct amd_iommu *iommu; |
525 | struct iommu_cmd cmd; | 635 | struct iommu_cmd cmd; |
526 | u16 devid; | 636 | u16 devid; |
637 | int qdep; | ||
527 | 638 | ||
639 | qdep = pci_ats_queue_depth(pdev); | ||
528 | devid = get_device_id(dev); | 640 | devid = get_device_id(dev); |
529 | iommu = amd_iommu_rlookup_table[devid]; | 641 | iommu = amd_iommu_rlookup_table[devid]; |
530 | 642 | ||
531 | /* Build command */ | 643 | build_inv_iotlb_pages(&cmd, devid, qdep, address, size); |
532 | memset(&cmd, 0, sizeof(cmd)); | ||
533 | CMD_SET_TYPE(&cmd, CMD_INV_DEV_ENTRY); | ||
534 | cmd.data[0] = devid; | ||
535 | 644 | ||
536 | return iommu_queue_command(iommu, &cmd); | 645 | return iommu_queue_command(iommu, &cmd); |
537 | } | 646 | } |
538 | 647 | ||
539 | static void __iommu_build_inv_iommu_pages(struct iommu_cmd *cmd, u64 address, | ||
540 | u16 domid, int pde, int s) | ||
541 | { | ||
542 | memset(cmd, 0, sizeof(*cmd)); | ||
543 | address &= PAGE_MASK; | ||
544 | CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES); | ||
545 | cmd->data[1] |= domid; | ||
546 | cmd->data[2] = lower_32_bits(address); | ||
547 | cmd->data[3] = upper_32_bits(address); | ||
548 | if (s) /* size bit - we flush more than one 4kb page */ | ||
549 | cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK; | ||
550 | if (pde) /* PDE bit - we wan't flush everything not only the PTEs */ | ||
551 | cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK; | ||
552 | } | ||
553 | |||
554 | /* | 648 | /* |
555 | * Generic command send function for invalidaing TLB entries | 649 | * Command send function for invalidating a device table entry |
556 | */ | 650 | */ |
557 | static int iommu_queue_inv_iommu_pages(struct amd_iommu *iommu, | 651 | static int device_flush_dte(struct device *dev) |
558 | u64 address, u16 domid, int pde, int s) | ||
559 | { | 652 | { |
560 | struct iommu_cmd cmd; | 653 | struct amd_iommu *iommu; |
654 | struct pci_dev *pdev; | ||
655 | u16 devid; | ||
561 | int ret; | 656 | int ret; |
562 | 657 | ||
563 | __iommu_build_inv_iommu_pages(&cmd, address, domid, pde, s); | 658 | pdev = to_pci_dev(dev); |
659 | devid = get_device_id(dev); | ||
660 | iommu = amd_iommu_rlookup_table[devid]; | ||
564 | 661 | ||
565 | ret = iommu_queue_command(iommu, &cmd); | 662 | ret = iommu_flush_dte(iommu, devid); |
663 | if (ret) | ||
664 | return ret; | ||
665 | |||
666 | if (pci_ats_enabled(pdev)) | ||
667 | ret = device_flush_iotlb(dev, 0, ~0UL); | ||
566 | 668 | ||
567 | return ret; | 669 | return ret; |
568 | } | 670 | } |
@@ -572,23 +674,14 @@ static int iommu_queue_inv_iommu_pages(struct amd_iommu *iommu, | |||
572 | * It invalidates a single PTE if the range to flush is within a single | 674 | * It invalidates a single PTE if the range to flush is within a single |
573 | * page. Otherwise it flushes the whole TLB of the IOMMU. | 675 | * page. Otherwise it flushes the whole TLB of the IOMMU. |
574 | */ | 676 | */ |
575 | static void __iommu_flush_pages(struct protection_domain *domain, | 677 | static void __domain_flush_pages(struct protection_domain *domain, |
576 | u64 address, size_t size, int pde) | 678 | u64 address, size_t size, int pde) |
577 | { | 679 | { |
578 | int s = 0, i; | 680 | struct iommu_dev_data *dev_data; |
579 | unsigned long pages = iommu_num_pages(address, size, PAGE_SIZE); | 681 | struct iommu_cmd cmd; |
580 | 682 | int ret = 0, i; | |
581 | address &= PAGE_MASK; | ||
582 | |||
583 | if (pages > 1) { | ||
584 | /* | ||
585 | * If we have to flush more than one page, flush all | ||
586 | * TLB entries for this domain | ||
587 | */ | ||
588 | address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS; | ||
589 | s = 1; | ||
590 | } | ||
591 | 683 | ||
684 | build_inv_iommu_pages(&cmd, address, size, domain->id, pde); | ||
592 | 685 | ||
593 | for (i = 0; i < amd_iommus_present; ++i) { | 686 | for (i = 0; i < amd_iommus_present; ++i) { |
594 | if (!domain->dev_iommu[i]) | 687 | if (!domain->dev_iommu[i]) |
@@ -598,101 +691,70 @@ static void __iommu_flush_pages(struct protection_domain *domain, | |||
598 | * Devices of this domain are behind this IOMMU | 691 | * Devices of this domain are behind this IOMMU |
599 | * We need a TLB flush | 692 | * We need a TLB flush |
600 | */ | 693 | */ |
601 | iommu_queue_inv_iommu_pages(amd_iommus[i], address, | 694 | ret |= iommu_queue_command(amd_iommus[i], &cmd); |
602 | domain->id, pde, s); | ||
603 | } | 695 | } |
604 | 696 | ||
605 | return; | 697 | list_for_each_entry(dev_data, &domain->dev_list, list) { |
698 | struct pci_dev *pdev = to_pci_dev(dev_data->dev); | ||
699 | |||
700 | if (!pci_ats_enabled(pdev)) | ||
701 | continue; | ||
702 | |||
703 | ret |= device_flush_iotlb(dev_data->dev, address, size); | ||
704 | } | ||
705 | |||
706 | WARN_ON(ret); | ||
606 | } | 707 | } |
607 | 708 | ||
608 | static void iommu_flush_pages(struct protection_domain *domain, | 709 | static void domain_flush_pages(struct protection_domain *domain, |
609 | u64 address, size_t size) | 710 | u64 address, size_t size) |
610 | { | 711 | { |
611 | __iommu_flush_pages(domain, address, size, 0); | 712 | __domain_flush_pages(domain, address, size, 0); |
612 | } | 713 | } |
613 | 714 | ||
614 | /* Flush the whole IO/TLB for a given protection domain */ | 715 | /* Flush the whole IO/TLB for a given protection domain */ |
615 | static void iommu_flush_tlb(struct protection_domain *domain) | 716 | static void domain_flush_tlb(struct protection_domain *domain) |
616 | { | 717 | { |
617 | __iommu_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 0); | 718 | __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 0); |
618 | } | 719 | } |
619 | 720 | ||
620 | /* Flush the whole IO/TLB for a given protection domain - including PDE */ | 721 | /* Flush the whole IO/TLB for a given protection domain - including PDE */ |
621 | static void iommu_flush_tlb_pde(struct protection_domain *domain) | 722 | static void domain_flush_tlb_pde(struct protection_domain *domain) |
622 | { | 723 | { |
623 | __iommu_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 1); | 724 | __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 1); |
624 | } | 725 | } |
625 | 726 | ||
626 | 727 | static void domain_flush_complete(struct protection_domain *domain) | |
627 | /* | ||
628 | * This function flushes the DTEs for all devices in domain | ||
629 | */ | ||
630 | static void iommu_flush_domain_devices(struct protection_domain *domain) | ||
631 | { | 728 | { |
632 | struct iommu_dev_data *dev_data; | 729 | int i; |
633 | unsigned long flags; | ||
634 | |||
635 | spin_lock_irqsave(&domain->lock, flags); | ||
636 | |||
637 | list_for_each_entry(dev_data, &domain->dev_list, list) | ||
638 | iommu_flush_device(dev_data->dev); | ||
639 | |||
640 | spin_unlock_irqrestore(&domain->lock, flags); | ||
641 | } | ||
642 | |||
643 | static void iommu_flush_all_domain_devices(void) | ||
644 | { | ||
645 | struct protection_domain *domain; | ||
646 | unsigned long flags; | ||
647 | 730 | ||
648 | spin_lock_irqsave(&amd_iommu_pd_lock, flags); | 731 | for (i = 0; i < amd_iommus_present; ++i) { |
732 | if (!domain->dev_iommu[i]) | ||
733 | continue; | ||
649 | 734 | ||
650 | list_for_each_entry(domain, &amd_iommu_pd_list, list) { | 735 | /* |
651 | iommu_flush_domain_devices(domain); | 736 | * Devices of this domain are behind this IOMMU |
652 | iommu_flush_complete(domain); | 737 | * We need to wait for completion of all commands. |
738 | */ | ||
739 | iommu_completion_wait(amd_iommus[i]); | ||
653 | } | 740 | } |
654 | |||
655 | spin_unlock_irqrestore(&amd_iommu_pd_lock, flags); | ||
656 | } | 741 | } |
657 | 742 | ||
658 | void amd_iommu_flush_all_devices(void) | ||
659 | { | ||
660 | iommu_flush_all_domain_devices(); | ||
661 | } | ||
662 | 743 | ||
663 | /* | 744 | /* |
664 | * This function uses heavy locking and may disable irqs for some time. But | 745 | * This function flushes the DTEs for all devices in domain |
665 | * this is no issue because it is only called during resume. | ||
666 | */ | 746 | */ |
667 | void amd_iommu_flush_all_domains(void) | 747 | static void domain_flush_devices(struct protection_domain *domain) |
668 | { | 748 | { |
669 | struct protection_domain *domain; | 749 | struct iommu_dev_data *dev_data; |
670 | unsigned long flags; | 750 | unsigned long flags; |
671 | 751 | ||
672 | spin_lock_irqsave(&amd_iommu_pd_lock, flags); | 752 | spin_lock_irqsave(&domain->lock, flags); |
673 | |||
674 | list_for_each_entry(domain, &amd_iommu_pd_list, list) { | ||
675 | spin_lock(&domain->lock); | ||
676 | iommu_flush_tlb_pde(domain); | ||
677 | iommu_flush_complete(domain); | ||
678 | spin_unlock(&domain->lock); | ||
679 | } | ||
680 | |||
681 | spin_unlock_irqrestore(&amd_iommu_pd_lock, flags); | ||
682 | } | ||
683 | |||
684 | static void reset_iommu_command_buffer(struct amd_iommu *iommu) | ||
685 | { | ||
686 | pr_err("AMD-Vi: Resetting IOMMU command buffer\n"); | ||
687 | |||
688 | if (iommu->reset_in_progress) | ||
689 | panic("AMD-Vi: ILLEGAL_COMMAND_ERROR while resetting command buffer\n"); | ||
690 | 753 | ||
691 | amd_iommu_reset_cmd_buffer(iommu); | 754 | list_for_each_entry(dev_data, &domain->dev_list, list) |
692 | amd_iommu_flush_all_devices(); | 755 | device_flush_dte(dev_data->dev); |
693 | amd_iommu_flush_all_domains(); | ||
694 | 756 | ||
695 | iommu->reset_in_progress = false; | 757 | spin_unlock_irqrestore(&domain->lock, flags); |
696 | } | 758 | } |
697 | 759 | ||
698 | /**************************************************************************** | 760 | /**************************************************************************** |
@@ -1410,17 +1472,22 @@ static bool dma_ops_domain(struct protection_domain *domain) | |||
1410 | return domain->flags & PD_DMA_OPS_MASK; | 1472 | return domain->flags & PD_DMA_OPS_MASK; |
1411 | } | 1473 | } |
1412 | 1474 | ||
1413 | static void set_dte_entry(u16 devid, struct protection_domain *domain) | 1475 | static void set_dte_entry(u16 devid, struct protection_domain *domain, bool ats) |
1414 | { | 1476 | { |
1415 | u64 pte_root = virt_to_phys(domain->pt_root); | 1477 | u64 pte_root = virt_to_phys(domain->pt_root); |
1478 | u32 flags = 0; | ||
1416 | 1479 | ||
1417 | pte_root |= (domain->mode & DEV_ENTRY_MODE_MASK) | 1480 | pte_root |= (domain->mode & DEV_ENTRY_MODE_MASK) |
1418 | << DEV_ENTRY_MODE_SHIFT; | 1481 | << DEV_ENTRY_MODE_SHIFT; |
1419 | pte_root |= IOMMU_PTE_IR | IOMMU_PTE_IW | IOMMU_PTE_P | IOMMU_PTE_TV; | 1482 | pte_root |= IOMMU_PTE_IR | IOMMU_PTE_IW | IOMMU_PTE_P | IOMMU_PTE_TV; |
1420 | 1483 | ||
1421 | amd_iommu_dev_table[devid].data[2] = domain->id; | 1484 | if (ats) |
1422 | amd_iommu_dev_table[devid].data[1] = upper_32_bits(pte_root); | 1485 | flags |= DTE_FLAG_IOTLB; |
1423 | amd_iommu_dev_table[devid].data[0] = lower_32_bits(pte_root); | 1486 | |
1487 | amd_iommu_dev_table[devid].data[3] |= flags; | ||
1488 | amd_iommu_dev_table[devid].data[2] = domain->id; | ||
1489 | amd_iommu_dev_table[devid].data[1] = upper_32_bits(pte_root); | ||
1490 | amd_iommu_dev_table[devid].data[0] = lower_32_bits(pte_root); | ||
1424 | } | 1491 | } |
1425 | 1492 | ||
1426 | static void clear_dte_entry(u16 devid) | 1493 | static void clear_dte_entry(u16 devid) |
@@ -1437,34 +1504,42 @@ static void do_attach(struct device *dev, struct protection_domain *domain) | |||
1437 | { | 1504 | { |
1438 | struct iommu_dev_data *dev_data; | 1505 | struct iommu_dev_data *dev_data; |
1439 | struct amd_iommu *iommu; | 1506 | struct amd_iommu *iommu; |
1507 | struct pci_dev *pdev; | ||
1508 | bool ats = false; | ||
1440 | u16 devid; | 1509 | u16 devid; |
1441 | 1510 | ||
1442 | devid = get_device_id(dev); | 1511 | devid = get_device_id(dev); |
1443 | iommu = amd_iommu_rlookup_table[devid]; | 1512 | iommu = amd_iommu_rlookup_table[devid]; |
1444 | dev_data = get_dev_data(dev); | 1513 | dev_data = get_dev_data(dev); |
1514 | pdev = to_pci_dev(dev); | ||
1515 | |||
1516 | if (amd_iommu_iotlb_sup) | ||
1517 | ats = pci_ats_enabled(pdev); | ||
1445 | 1518 | ||
1446 | /* Update data structures */ | 1519 | /* Update data structures */ |
1447 | dev_data->domain = domain; | 1520 | dev_data->domain = domain; |
1448 | list_add(&dev_data->list, &domain->dev_list); | 1521 | list_add(&dev_data->list, &domain->dev_list); |
1449 | set_dte_entry(devid, domain); | 1522 | set_dte_entry(devid, domain, ats); |
1450 | 1523 | ||
1451 | /* Do reference counting */ | 1524 | /* Do reference counting */ |
1452 | domain->dev_iommu[iommu->index] += 1; | 1525 | domain->dev_iommu[iommu->index] += 1; |
1453 | domain->dev_cnt += 1; | 1526 | domain->dev_cnt += 1; |
1454 | 1527 | ||
1455 | /* Flush the DTE entry */ | 1528 | /* Flush the DTE entry */ |
1456 | iommu_flush_device(dev); | 1529 | device_flush_dte(dev); |
1457 | } | 1530 | } |
1458 | 1531 | ||
1459 | static void do_detach(struct device *dev) | 1532 | static void do_detach(struct device *dev) |
1460 | { | 1533 | { |
1461 | struct iommu_dev_data *dev_data; | 1534 | struct iommu_dev_data *dev_data; |
1462 | struct amd_iommu *iommu; | 1535 | struct amd_iommu *iommu; |
1536 | struct pci_dev *pdev; | ||
1463 | u16 devid; | 1537 | u16 devid; |
1464 | 1538 | ||
1465 | devid = get_device_id(dev); | 1539 | devid = get_device_id(dev); |
1466 | iommu = amd_iommu_rlookup_table[devid]; | 1540 | iommu = amd_iommu_rlookup_table[devid]; |
1467 | dev_data = get_dev_data(dev); | 1541 | dev_data = get_dev_data(dev); |
1542 | pdev = to_pci_dev(dev); | ||
1468 | 1543 | ||
1469 | /* decrease reference counters */ | 1544 | /* decrease reference counters */ |
1470 | dev_data->domain->dev_iommu[iommu->index] -= 1; | 1545 | dev_data->domain->dev_iommu[iommu->index] -= 1; |
@@ -1476,7 +1551,7 @@ static void do_detach(struct device *dev) | |||
1476 | clear_dte_entry(devid); | 1551 | clear_dte_entry(devid); |
1477 | 1552 | ||
1478 | /* Flush the DTE entry */ | 1553 | /* Flush the DTE entry */ |
1479 | iommu_flush_device(dev); | 1554 | device_flush_dte(dev); |
1480 | } | 1555 | } |
1481 | 1556 | ||
1482 | /* | 1557 | /* |
@@ -1539,9 +1614,13 @@ out_unlock: | |||
1539 | static int attach_device(struct device *dev, | 1614 | static int attach_device(struct device *dev, |
1540 | struct protection_domain *domain) | 1615 | struct protection_domain *domain) |
1541 | { | 1616 | { |
1617 | struct pci_dev *pdev = to_pci_dev(dev); | ||
1542 | unsigned long flags; | 1618 | unsigned long flags; |
1543 | int ret; | 1619 | int ret; |
1544 | 1620 | ||
1621 | if (amd_iommu_iotlb_sup) | ||
1622 | pci_enable_ats(pdev, PAGE_SHIFT); | ||
1623 | |||
1545 | write_lock_irqsave(&amd_iommu_devtable_lock, flags); | 1624 | write_lock_irqsave(&amd_iommu_devtable_lock, flags); |
1546 | ret = __attach_device(dev, domain); | 1625 | ret = __attach_device(dev, domain); |
1547 | write_unlock_irqrestore(&amd_iommu_devtable_lock, flags); | 1626 | write_unlock_irqrestore(&amd_iommu_devtable_lock, flags); |
@@ -1551,7 +1630,7 @@ static int attach_device(struct device *dev, | |||
1551 | * left the caches in the IOMMU dirty. So we have to flush | 1630 | * left the caches in the IOMMU dirty. So we have to flush |
1552 | * here to evict all dirty stuff. | 1631 | * here to evict all dirty stuff. |
1553 | */ | 1632 | */ |
1554 | iommu_flush_tlb_pde(domain); | 1633 | domain_flush_tlb_pde(domain); |
1555 | 1634 | ||
1556 | return ret; | 1635 | return ret; |
1557 | } | 1636 | } |
@@ -1598,12 +1677,16 @@ static void __detach_device(struct device *dev) | |||
1598 | */ | 1677 | */ |
1599 | static void detach_device(struct device *dev) | 1678 | static void detach_device(struct device *dev) |
1600 | { | 1679 | { |
1680 | struct pci_dev *pdev = to_pci_dev(dev); | ||
1601 | unsigned long flags; | 1681 | unsigned long flags; |
1602 | 1682 | ||
1603 | /* lock device table */ | 1683 | /* lock device table */ |
1604 | write_lock_irqsave(&amd_iommu_devtable_lock, flags); | 1684 | write_lock_irqsave(&amd_iommu_devtable_lock, flags); |
1605 | __detach_device(dev); | 1685 | __detach_device(dev); |
1606 | write_unlock_irqrestore(&amd_iommu_devtable_lock, flags); | 1686 | write_unlock_irqrestore(&amd_iommu_devtable_lock, flags); |
1687 | |||
1688 | if (amd_iommu_iotlb_sup && pci_ats_enabled(pdev)) | ||
1689 | pci_disable_ats(pdev); | ||
1607 | } | 1690 | } |
1608 | 1691 | ||
1609 | /* | 1692 | /* |
@@ -1692,7 +1775,7 @@ static int device_change_notifier(struct notifier_block *nb, | |||
1692 | goto out; | 1775 | goto out; |
1693 | } | 1776 | } |
1694 | 1777 | ||
1695 | iommu_flush_device(dev); | 1778 | device_flush_dte(dev); |
1696 | iommu_completion_wait(iommu); | 1779 | iommu_completion_wait(iommu); |
1697 | 1780 | ||
1698 | out: | 1781 | out: |
@@ -1753,8 +1836,9 @@ static void update_device_table(struct protection_domain *domain) | |||
1753 | struct iommu_dev_data *dev_data; | 1836 | struct iommu_dev_data *dev_data; |
1754 | 1837 | ||
1755 | list_for_each_entry(dev_data, &domain->dev_list, list) { | 1838 | list_for_each_entry(dev_data, &domain->dev_list, list) { |
1839 | struct pci_dev *pdev = to_pci_dev(dev_data->dev); | ||
1756 | u16 devid = get_device_id(dev_data->dev); | 1840 | u16 devid = get_device_id(dev_data->dev); |
1757 | set_dte_entry(devid, domain); | 1841 | set_dte_entry(devid, domain, pci_ats_enabled(pdev)); |
1758 | } | 1842 | } |
1759 | } | 1843 | } |
1760 | 1844 | ||
@@ -1764,8 +1848,9 @@ static void update_domain(struct protection_domain *domain) | |||
1764 | return; | 1848 | return; |
1765 | 1849 | ||
1766 | update_device_table(domain); | 1850 | update_device_table(domain); |
1767 | iommu_flush_domain_devices(domain); | 1851 | |
1768 | iommu_flush_tlb_pde(domain); | 1852 | domain_flush_devices(domain); |
1853 | domain_flush_tlb_pde(domain); | ||
1769 | 1854 | ||
1770 | domain->updated = false; | 1855 | domain->updated = false; |
1771 | } | 1856 | } |
@@ -1924,10 +2009,10 @@ retry: | |||
1924 | ADD_STATS_COUNTER(alloced_io_mem, size); | 2009 | ADD_STATS_COUNTER(alloced_io_mem, size); |
1925 | 2010 | ||
1926 | if (unlikely(dma_dom->need_flush && !amd_iommu_unmap_flush)) { | 2011 | if (unlikely(dma_dom->need_flush && !amd_iommu_unmap_flush)) { |
1927 | iommu_flush_tlb(&dma_dom->domain); | 2012 | domain_flush_tlb(&dma_dom->domain); |
1928 | dma_dom->need_flush = false; | 2013 | dma_dom->need_flush = false; |
1929 | } else if (unlikely(amd_iommu_np_cache)) | 2014 | } else if (unlikely(amd_iommu_np_cache)) |
1930 | iommu_flush_pages(&dma_dom->domain, address, size); | 2015 | domain_flush_pages(&dma_dom->domain, address, size); |
1931 | 2016 | ||
1932 | out: | 2017 | out: |
1933 | return address; | 2018 | return address; |
@@ -1976,7 +2061,7 @@ static void __unmap_single(struct dma_ops_domain *dma_dom, | |||
1976 | dma_ops_free_addresses(dma_dom, dma_addr, pages); | 2061 | dma_ops_free_addresses(dma_dom, dma_addr, pages); |
1977 | 2062 | ||
1978 | if (amd_iommu_unmap_flush || dma_dom->need_flush) { | 2063 | if (amd_iommu_unmap_flush || dma_dom->need_flush) { |
1979 | iommu_flush_pages(&dma_dom->domain, flush_addr, size); | 2064 | domain_flush_pages(&dma_dom->domain, flush_addr, size); |
1980 | dma_dom->need_flush = false; | 2065 | dma_dom->need_flush = false; |
1981 | } | 2066 | } |
1982 | } | 2067 | } |
@@ -2012,7 +2097,7 @@ static dma_addr_t map_page(struct device *dev, struct page *page, | |||
2012 | if (addr == DMA_ERROR_CODE) | 2097 | if (addr == DMA_ERROR_CODE) |
2013 | goto out; | 2098 | goto out; |
2014 | 2099 | ||
2015 | iommu_flush_complete(domain); | 2100 | domain_flush_complete(domain); |
2016 | 2101 | ||
2017 | out: | 2102 | out: |
2018 | spin_unlock_irqrestore(&domain->lock, flags); | 2103 | spin_unlock_irqrestore(&domain->lock, flags); |
@@ -2039,7 +2124,7 @@ static void unmap_page(struct device *dev, dma_addr_t dma_addr, size_t size, | |||
2039 | 2124 | ||
2040 | __unmap_single(domain->priv, dma_addr, size, dir); | 2125 | __unmap_single(domain->priv, dma_addr, size, dir); |
2041 | 2126 | ||
2042 | iommu_flush_complete(domain); | 2127 | domain_flush_complete(domain); |
2043 | 2128 | ||
2044 | spin_unlock_irqrestore(&domain->lock, flags); | 2129 | spin_unlock_irqrestore(&domain->lock, flags); |
2045 | } | 2130 | } |
@@ -2104,7 +2189,7 @@ static int map_sg(struct device *dev, struct scatterlist *sglist, | |||
2104 | goto unmap; | 2189 | goto unmap; |
2105 | } | 2190 | } |
2106 | 2191 | ||
2107 | iommu_flush_complete(domain); | 2192 | domain_flush_complete(domain); |
2108 | 2193 | ||
2109 | out: | 2194 | out: |
2110 | spin_unlock_irqrestore(&domain->lock, flags); | 2195 | spin_unlock_irqrestore(&domain->lock, flags); |
@@ -2150,7 +2235,7 @@ static void unmap_sg(struct device *dev, struct scatterlist *sglist, | |||
2150 | s->dma_address = s->dma_length = 0; | 2235 | s->dma_address = s->dma_length = 0; |
2151 | } | 2236 | } |
2152 | 2237 | ||
2153 | iommu_flush_complete(domain); | 2238 | domain_flush_complete(domain); |
2154 | 2239 | ||
2155 | spin_unlock_irqrestore(&domain->lock, flags); | 2240 | spin_unlock_irqrestore(&domain->lock, flags); |
2156 | } | 2241 | } |
@@ -2200,7 +2285,7 @@ static void *alloc_coherent(struct device *dev, size_t size, | |||
2200 | goto out_free; | 2285 | goto out_free; |
2201 | } | 2286 | } |
2202 | 2287 | ||
2203 | iommu_flush_complete(domain); | 2288 | domain_flush_complete(domain); |
2204 | 2289 | ||
2205 | spin_unlock_irqrestore(&domain->lock, flags); | 2290 | spin_unlock_irqrestore(&domain->lock, flags); |
2206 | 2291 | ||
@@ -2232,7 +2317,7 @@ static void free_coherent(struct device *dev, size_t size, | |||
2232 | 2317 | ||
2233 | __unmap_single(domain->priv, dma_addr, size, DMA_BIDIRECTIONAL); | 2318 | __unmap_single(domain->priv, dma_addr, size, DMA_BIDIRECTIONAL); |
2234 | 2319 | ||
2235 | iommu_flush_complete(domain); | 2320 | domain_flush_complete(domain); |
2236 | 2321 | ||
2237 | spin_unlock_irqrestore(&domain->lock, flags); | 2322 | spin_unlock_irqrestore(&domain->lock, flags); |
2238 | 2323 | ||
@@ -2476,7 +2561,7 @@ static void amd_iommu_detach_device(struct iommu_domain *dom, | |||
2476 | if (!iommu) | 2561 | if (!iommu) |
2477 | return; | 2562 | return; |
2478 | 2563 | ||
2479 | iommu_flush_device(dev); | 2564 | device_flush_dte(dev); |
2480 | iommu_completion_wait(iommu); | 2565 | iommu_completion_wait(iommu); |
2481 | } | 2566 | } |
2482 | 2567 | ||
@@ -2542,7 +2627,7 @@ static int amd_iommu_unmap(struct iommu_domain *dom, unsigned long iova, | |||
2542 | unmap_size = iommu_unmap_page(domain, iova, page_size); | 2627 | unmap_size = iommu_unmap_page(domain, iova, page_size); |
2543 | mutex_unlock(&domain->api_lock); | 2628 | mutex_unlock(&domain->api_lock); |
2544 | 2629 | ||
2545 | iommu_flush_tlb_pde(domain); | 2630 | domain_flush_tlb_pde(domain); |
2546 | 2631 | ||
2547 | return get_order(unmap_size); | 2632 | return get_order(unmap_size); |
2548 | } | 2633 | } |