2 * Copyright (C) 2007-2008 Advanced Micro Devices, Inc.
3 * Author: Joerg Roedel <joerg.roedel@amd.com>
4 * Leo Duran <leo.duran@amd.com>
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published
8 * by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #include <linux/pci.h>
21 #include <linux/gfp.h>
22 #include <linux/bitops.h>
23 #include <linux/debugfs.h>
24 #include <linux/scatterlist.h>
25 #include <linux/iommu-helper.h>
26 #ifdef CONFIG_IOMMU_API
27 #include <linux/iommu.h>
29 #include <asm/proto.h>
30 #include <asm/iommu.h>
32 #include <asm/amd_iommu_types.h>
33 #include <asm/amd_iommu.h>
35 #define CMD_SET_TYPE(cmd, t) ((cmd)->data[1] |= ((t) << 28))
37 #define EXIT_LOOP_COUNT 10000000
39 static DEFINE_RWLOCK(amd_iommu_devtable_lock
);
41 /* A list of preallocated protection domains */
42 static LIST_HEAD(iommu_pd_list
);
43 static DEFINE_SPINLOCK(iommu_pd_list_lock
);
45 #ifdef CONFIG_IOMMU_API
46 static struct iommu_ops amd_iommu_ops
;
50 * general struct to manage commands send to an IOMMU
56 static int dma_ops_unity_map(struct dma_ops_domain
*dma_dom
,
57 struct unity_map_entry
*e
);
58 static struct dma_ops_domain
*find_protection_domain(u16 devid
);
61 #ifdef CONFIG_AMD_IOMMU_STATS
64 * Initialization code for statistics collection
67 DECLARE_STATS_COUNTER(compl_wait
);
69 static struct dentry
*stats_dir
;
70 static struct dentry
*de_isolate
;
71 static struct dentry
*de_fflush
;
73 static void amd_iommu_stats_add(struct __iommu_counter
*cnt
)
75 if (stats_dir
== NULL
)
78 cnt
->dent
= debugfs_create_u64(cnt
->name
, 0444, stats_dir
,
82 static void amd_iommu_stats_init(void)
84 stats_dir
= debugfs_create_dir("amd-iommu", NULL
);
85 if (stats_dir
== NULL
)
88 de_isolate
= debugfs_create_bool("isolation", 0444, stats_dir
,
89 (u32
*)&amd_iommu_isolate
);
91 de_fflush
= debugfs_create_bool("fullflush", 0444, stats_dir
,
92 (u32
*)&amd_iommu_unmap_flush
);
94 amd_iommu_stats_add(&compl_wait
);
99 /* returns !0 if the IOMMU is caching non-present entries in its TLB */
100 static int iommu_has_npcache(struct amd_iommu
*iommu
)
102 return iommu
->cap
& (1UL << IOMMU_CAP_NPCACHE
);
105 /****************************************************************************
107 * Interrupt handling functions
109 ****************************************************************************/
111 static void iommu_print_event(void *__evt
)
114 int type
= (event
[1] >> EVENT_TYPE_SHIFT
) & EVENT_TYPE_MASK
;
115 int devid
= (event
[0] >> EVENT_DEVID_SHIFT
) & EVENT_DEVID_MASK
;
116 int domid
= (event
[1] >> EVENT_DOMID_SHIFT
) & EVENT_DOMID_MASK
;
117 int flags
= (event
[1] >> EVENT_FLAGS_SHIFT
) & EVENT_FLAGS_MASK
;
118 u64 address
= (u64
)(((u64
)event
[3]) << 32) | event
[2];
120 printk(KERN_ERR
"AMD IOMMU: Event logged [");
123 case EVENT_TYPE_ILL_DEV
:
124 printk("ILLEGAL_DEV_TABLE_ENTRY device=%02x:%02x.%x "
125 "address=0x%016llx flags=0x%04x]\n",
126 PCI_BUS(devid
), PCI_SLOT(devid
), PCI_FUNC(devid
),
129 case EVENT_TYPE_IO_FAULT
:
130 printk("IO_PAGE_FAULT device=%02x:%02x.%x "
131 "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
132 PCI_BUS(devid
), PCI_SLOT(devid
), PCI_FUNC(devid
),
133 domid
, address
, flags
);
135 case EVENT_TYPE_DEV_TAB_ERR
:
136 printk("DEV_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
137 "address=0x%016llx flags=0x%04x]\n",
138 PCI_BUS(devid
), PCI_SLOT(devid
), PCI_FUNC(devid
),
141 case EVENT_TYPE_PAGE_TAB_ERR
:
142 printk("PAGE_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
143 "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
144 PCI_BUS(devid
), PCI_SLOT(devid
), PCI_FUNC(devid
),
145 domid
, address
, flags
);
147 case EVENT_TYPE_ILL_CMD
:
148 printk("ILLEGAL_COMMAND_ERROR address=0x%016llx]\n", address
);
150 case EVENT_TYPE_CMD_HARD_ERR
:
151 printk("COMMAND_HARDWARE_ERROR address=0x%016llx "
152 "flags=0x%04x]\n", address
, flags
);
154 case EVENT_TYPE_IOTLB_INV_TO
:
155 printk("IOTLB_INV_TIMEOUT device=%02x:%02x.%x "
156 "address=0x%016llx]\n",
157 PCI_BUS(devid
), PCI_SLOT(devid
), PCI_FUNC(devid
),
160 case EVENT_TYPE_INV_DEV_REQ
:
161 printk("INVALID_DEVICE_REQUEST device=%02x:%02x.%x "
162 "address=0x%016llx flags=0x%04x]\n",
163 PCI_BUS(devid
), PCI_SLOT(devid
), PCI_FUNC(devid
),
167 printk(KERN_ERR
"UNKNOWN type=0x%02x]\n", type
);
171 static void iommu_poll_events(struct amd_iommu
*iommu
)
176 spin_lock_irqsave(&iommu
->lock
, flags
);
178 head
= readl(iommu
->mmio_base
+ MMIO_EVT_HEAD_OFFSET
);
179 tail
= readl(iommu
->mmio_base
+ MMIO_EVT_TAIL_OFFSET
);
181 while (head
!= tail
) {
182 iommu_print_event(iommu
->evt_buf
+ head
);
183 head
= (head
+ EVENT_ENTRY_SIZE
) % iommu
->evt_buf_size
;
186 writel(head
, iommu
->mmio_base
+ MMIO_EVT_HEAD_OFFSET
);
188 spin_unlock_irqrestore(&iommu
->lock
, flags
);
191 irqreturn_t
amd_iommu_int_handler(int irq
, void *data
)
193 struct amd_iommu
*iommu
;
195 list_for_each_entry(iommu
, &amd_iommu_list
, list
)
196 iommu_poll_events(iommu
);
201 /****************************************************************************
203 * IOMMU command queuing functions
205 ****************************************************************************/
208 * Writes the command to the IOMMUs command buffer and informs the
209 * hardware about the new command. Must be called with iommu->lock held.
211 static int __iommu_queue_command(struct amd_iommu
*iommu
, struct iommu_cmd
*cmd
)
216 tail
= readl(iommu
->mmio_base
+ MMIO_CMD_TAIL_OFFSET
);
217 target
= iommu
->cmd_buf
+ tail
;
218 memcpy_toio(target
, cmd
, sizeof(*cmd
));
219 tail
= (tail
+ sizeof(*cmd
)) % iommu
->cmd_buf_size
;
220 head
= readl(iommu
->mmio_base
+ MMIO_CMD_HEAD_OFFSET
);
223 writel(tail
, iommu
->mmio_base
+ MMIO_CMD_TAIL_OFFSET
);
229 * General queuing function for commands. Takes iommu->lock and calls
230 * __iommu_queue_command().
232 static int iommu_queue_command(struct amd_iommu
*iommu
, struct iommu_cmd
*cmd
)
237 spin_lock_irqsave(&iommu
->lock
, flags
);
238 ret
= __iommu_queue_command(iommu
, cmd
);
240 iommu
->need_sync
= true;
241 spin_unlock_irqrestore(&iommu
->lock
, flags
);
247 * This function waits until an IOMMU has completed a completion
250 static void __iommu_wait_for_completion(struct amd_iommu
*iommu
)
256 INC_STATS_COUNTER(compl_wait
);
258 while (!ready
&& (i
< EXIT_LOOP_COUNT
)) {
260 /* wait for the bit to become one */
261 status
= readl(iommu
->mmio_base
+ MMIO_STATUS_OFFSET
);
262 ready
= status
& MMIO_STATUS_COM_WAIT_INT_MASK
;
265 /* set bit back to zero */
266 status
&= ~MMIO_STATUS_COM_WAIT_INT_MASK
;
267 writel(status
, iommu
->mmio_base
+ MMIO_STATUS_OFFSET
);
269 if (unlikely(i
== EXIT_LOOP_COUNT
))
270 panic("AMD IOMMU: Completion wait loop failed\n");
274 * This function queues a completion wait command into the command
277 static int __iommu_completion_wait(struct amd_iommu
*iommu
)
279 struct iommu_cmd cmd
;
281 memset(&cmd
, 0, sizeof(cmd
));
282 cmd
.data
[0] = CMD_COMPL_WAIT_INT_MASK
;
283 CMD_SET_TYPE(&cmd
, CMD_COMPL_WAIT
);
285 return __iommu_queue_command(iommu
, &cmd
);
289 * This function is called whenever we need to ensure that the IOMMU has
290 * completed execution of all commands we sent. It sends a
291 * COMPLETION_WAIT command and waits for it to finish. The IOMMU informs
292 * us about that by writing a value to a physical address we pass with
295 static int iommu_completion_wait(struct amd_iommu
*iommu
)
300 spin_lock_irqsave(&iommu
->lock
, flags
);
302 if (!iommu
->need_sync
)
305 ret
= __iommu_completion_wait(iommu
);
307 iommu
->need_sync
= false;
312 __iommu_wait_for_completion(iommu
);
315 spin_unlock_irqrestore(&iommu
->lock
, flags
);
321 * Command send function for invalidating a device table entry
323 static int iommu_queue_inv_dev_entry(struct amd_iommu
*iommu
, u16 devid
)
325 struct iommu_cmd cmd
;
328 BUG_ON(iommu
== NULL
);
330 memset(&cmd
, 0, sizeof(cmd
));
331 CMD_SET_TYPE(&cmd
, CMD_INV_DEV_ENTRY
);
334 ret
= iommu_queue_command(iommu
, &cmd
);
339 static void __iommu_build_inv_iommu_pages(struct iommu_cmd
*cmd
, u64 address
,
340 u16 domid
, int pde
, int s
)
342 memset(cmd
, 0, sizeof(*cmd
));
343 address
&= PAGE_MASK
;
344 CMD_SET_TYPE(cmd
, CMD_INV_IOMMU_PAGES
);
345 cmd
->data
[1] |= domid
;
346 cmd
->data
[2] = lower_32_bits(address
);
347 cmd
->data
[3] = upper_32_bits(address
);
348 if (s
) /* size bit - we flush more than one 4kb page */
349 cmd
->data
[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK
;
350 if (pde
) /* PDE bit - we wan't flush everything not only the PTEs */
351 cmd
->data
[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK
;
355 * Generic command send function for invalidaing TLB entries
357 static int iommu_queue_inv_iommu_pages(struct amd_iommu
*iommu
,
358 u64 address
, u16 domid
, int pde
, int s
)
360 struct iommu_cmd cmd
;
363 __iommu_build_inv_iommu_pages(&cmd
, address
, domid
, pde
, s
);
365 ret
= iommu_queue_command(iommu
, &cmd
);
371 * TLB invalidation function which is called from the mapping functions.
372 * It invalidates a single PTE if the range to flush is within a single
373 * page. Otherwise it flushes the whole TLB of the IOMMU.
375 static int iommu_flush_pages(struct amd_iommu
*iommu
, u16 domid
,
376 u64 address
, size_t size
)
379 unsigned pages
= iommu_num_pages(address
, size
, PAGE_SIZE
);
381 address
&= PAGE_MASK
;
385 * If we have to flush more than one page, flush all
386 * TLB entries for this domain
388 address
= CMD_INV_IOMMU_ALL_PAGES_ADDRESS
;
392 iommu_queue_inv_iommu_pages(iommu
, address
, domid
, 0, s
);
397 /* Flush the whole IO/TLB for a given protection domain */
398 static void iommu_flush_tlb(struct amd_iommu
*iommu
, u16 domid
)
400 u64 address
= CMD_INV_IOMMU_ALL_PAGES_ADDRESS
;
402 iommu_queue_inv_iommu_pages(iommu
, address
, domid
, 0, 1);
405 #ifdef CONFIG_IOMMU_API
407 * This function is used to flush the IO/TLB for a given protection domain
408 * on every IOMMU in the system
410 static void iommu_flush_domain(u16 domid
)
413 struct amd_iommu
*iommu
;
414 struct iommu_cmd cmd
;
416 __iommu_build_inv_iommu_pages(&cmd
, CMD_INV_IOMMU_ALL_PAGES_ADDRESS
,
419 list_for_each_entry(iommu
, &amd_iommu_list
, list
) {
420 spin_lock_irqsave(&iommu
->lock
, flags
);
421 __iommu_queue_command(iommu
, &cmd
);
422 __iommu_completion_wait(iommu
);
423 __iommu_wait_for_completion(iommu
);
424 spin_unlock_irqrestore(&iommu
->lock
, flags
);
429 /****************************************************************************
431 * The functions below are used the create the page table mappings for
432 * unity mapped regions.
434 ****************************************************************************/
437 * Generic mapping functions. It maps a physical address into a DMA
438 * address space. It allocates the page table pages if necessary.
439 * In the future it can be extended to a generic mapping function
440 * supporting all features of AMD IOMMU page tables like level skipping
441 * and full 64 bit address spaces.
443 static int iommu_map_page(struct protection_domain
*dom
,
444 unsigned long bus_addr
,
445 unsigned long phys_addr
,
448 u64 __pte
, *pte
, *page
;
450 bus_addr
= PAGE_ALIGN(bus_addr
);
451 phys_addr
= PAGE_ALIGN(phys_addr
);
453 /* only support 512GB address spaces for now */
454 if (bus_addr
> IOMMU_MAP_SIZE_L3
|| !(prot
& IOMMU_PROT_MASK
))
457 pte
= &dom
->pt_root
[IOMMU_PTE_L2_INDEX(bus_addr
)];
459 if (!IOMMU_PTE_PRESENT(*pte
)) {
460 page
= (u64
*)get_zeroed_page(GFP_KERNEL
);
463 *pte
= IOMMU_L2_PDE(virt_to_phys(page
));
466 pte
= IOMMU_PTE_PAGE(*pte
);
467 pte
= &pte
[IOMMU_PTE_L1_INDEX(bus_addr
)];
469 if (!IOMMU_PTE_PRESENT(*pte
)) {
470 page
= (u64
*)get_zeroed_page(GFP_KERNEL
);
473 *pte
= IOMMU_L1_PDE(virt_to_phys(page
));
476 pte
= IOMMU_PTE_PAGE(*pte
);
477 pte
= &pte
[IOMMU_PTE_L0_INDEX(bus_addr
)];
479 if (IOMMU_PTE_PRESENT(*pte
))
482 __pte
= phys_addr
| IOMMU_PTE_P
;
483 if (prot
& IOMMU_PROT_IR
)
484 __pte
|= IOMMU_PTE_IR
;
485 if (prot
& IOMMU_PROT_IW
)
486 __pte
|= IOMMU_PTE_IW
;
493 #ifdef CONFIG_IOMMU_API
494 static void iommu_unmap_page(struct protection_domain
*dom
,
495 unsigned long bus_addr
)
499 pte
= &dom
->pt_root
[IOMMU_PTE_L2_INDEX(bus_addr
)];
501 if (!IOMMU_PTE_PRESENT(*pte
))
504 pte
= IOMMU_PTE_PAGE(*pte
);
505 pte
= &pte
[IOMMU_PTE_L1_INDEX(bus_addr
)];
507 if (!IOMMU_PTE_PRESENT(*pte
))
510 pte
= IOMMU_PTE_PAGE(*pte
);
511 pte
= &pte
[IOMMU_PTE_L1_INDEX(bus_addr
)];
518 * This function checks if a specific unity mapping entry is needed for
519 * this specific IOMMU.
521 static int iommu_for_unity_map(struct amd_iommu
*iommu
,
522 struct unity_map_entry
*entry
)
526 for (i
= entry
->devid_start
; i
<= entry
->devid_end
; ++i
) {
527 bdf
= amd_iommu_alias_table
[i
];
528 if (amd_iommu_rlookup_table
[bdf
] == iommu
)
536 * Init the unity mappings for a specific IOMMU in the system
538 * Basically iterates over all unity mapping entries and applies them to
539 * the default domain DMA of that IOMMU if necessary.
541 static int iommu_init_unity_mappings(struct amd_iommu
*iommu
)
543 struct unity_map_entry
*entry
;
546 list_for_each_entry(entry
, &amd_iommu_unity_map
, list
) {
547 if (!iommu_for_unity_map(iommu
, entry
))
549 ret
= dma_ops_unity_map(iommu
->default_dom
, entry
);
558 * This function actually applies the mapping to the page table of the
561 static int dma_ops_unity_map(struct dma_ops_domain
*dma_dom
,
562 struct unity_map_entry
*e
)
567 for (addr
= e
->address_start
; addr
< e
->address_end
;
569 ret
= iommu_map_page(&dma_dom
->domain
, addr
, addr
, e
->prot
);
573 * if unity mapping is in aperture range mark the page
574 * as allocated in the aperture
576 if (addr
< dma_dom
->aperture_size
)
577 __set_bit(addr
>> PAGE_SHIFT
, dma_dom
->bitmap
);
584 * Inits the unity mappings required for a specific device
586 static int init_unity_mappings_for_device(struct dma_ops_domain
*dma_dom
,
589 struct unity_map_entry
*e
;
592 list_for_each_entry(e
, &amd_iommu_unity_map
, list
) {
593 if (!(devid
>= e
->devid_start
&& devid
<= e
->devid_end
))
595 ret
= dma_ops_unity_map(dma_dom
, e
);
603 /****************************************************************************
605 * The next functions belong to the address allocator for the dma_ops
606 * interface functions. They work like the allocators in the other IOMMU
607 * drivers. Its basically a bitmap which marks the allocated pages in
608 * the aperture. Maybe it could be enhanced in the future to a more
609 * efficient allocator.
611 ****************************************************************************/
614 * The address allocator core function.
616 * called with domain->lock held
618 static unsigned long dma_ops_alloc_addresses(struct device
*dev
,
619 struct dma_ops_domain
*dom
,
621 unsigned long align_mask
,
625 unsigned long address
;
626 unsigned long boundary_size
;
628 boundary_size
= ALIGN(dma_get_seg_boundary(dev
) + 1,
629 PAGE_SIZE
) >> PAGE_SHIFT
;
630 limit
= iommu_device_max_index(dom
->aperture_size
>> PAGE_SHIFT
, 0,
631 dma_mask
>> PAGE_SHIFT
);
633 if (dom
->next_bit
>= limit
) {
635 dom
->need_flush
= true;
638 address
= iommu_area_alloc(dom
->bitmap
, limit
, dom
->next_bit
, pages
,
639 0 , boundary_size
, align_mask
);
641 address
= iommu_area_alloc(dom
->bitmap
, limit
, 0, pages
,
642 0, boundary_size
, align_mask
);
643 dom
->need_flush
= true;
646 if (likely(address
!= -1)) {
647 dom
->next_bit
= address
+ pages
;
648 address
<<= PAGE_SHIFT
;
650 address
= bad_dma_address
;
652 WARN_ON((address
+ (PAGE_SIZE
*pages
)) > dom
->aperture_size
);
658 * The address free function.
660 * called with domain->lock held
662 static void dma_ops_free_addresses(struct dma_ops_domain
*dom
,
663 unsigned long address
,
666 address
>>= PAGE_SHIFT
;
667 iommu_area_free(dom
->bitmap
, address
, pages
);
669 if (address
>= dom
->next_bit
)
670 dom
->need_flush
= true;
673 /****************************************************************************
675 * The next functions belong to the domain allocation. A domain is
676 * allocated for every IOMMU as the default domain. If device isolation
677 * is enabled, every device get its own domain. The most important thing
678 * about domains is the page table mapping the DMA address space they
681 ****************************************************************************/
683 static u16
domain_id_alloc(void)
688 write_lock_irqsave(&amd_iommu_devtable_lock
, flags
);
689 id
= find_first_zero_bit(amd_iommu_pd_alloc_bitmap
, MAX_DOMAIN_ID
);
691 if (id
> 0 && id
< MAX_DOMAIN_ID
)
692 __set_bit(id
, amd_iommu_pd_alloc_bitmap
);
695 write_unlock_irqrestore(&amd_iommu_devtable_lock
, flags
);
700 #ifdef CONFIG_IOMMU_API
701 static void domain_id_free(int id
)
705 write_lock_irqsave(&amd_iommu_devtable_lock
, flags
);
706 if (id
> 0 && id
< MAX_DOMAIN_ID
)
707 __clear_bit(id
, amd_iommu_pd_alloc_bitmap
);
708 write_unlock_irqrestore(&amd_iommu_devtable_lock
, flags
);
713 * Used to reserve address ranges in the aperture (e.g. for exclusion
716 static void dma_ops_reserve_addresses(struct dma_ops_domain
*dom
,
717 unsigned long start_page
,
720 unsigned int last_page
= dom
->aperture_size
>> PAGE_SHIFT
;
722 if (start_page
+ pages
> last_page
)
723 pages
= last_page
- start_page
;
725 iommu_area_reserve(dom
->bitmap
, start_page
, pages
);
728 static void free_pagetable(struct protection_domain
*domain
)
733 p1
= domain
->pt_root
;
738 for (i
= 0; i
< 512; ++i
) {
739 if (!IOMMU_PTE_PRESENT(p1
[i
]))
742 p2
= IOMMU_PTE_PAGE(p1
[i
]);
743 for (j
= 0; j
< 512; ++j
) {
744 if (!IOMMU_PTE_PRESENT(p2
[j
]))
746 p3
= IOMMU_PTE_PAGE(p2
[j
]);
747 free_page((unsigned long)p3
);
750 free_page((unsigned long)p2
);
753 free_page((unsigned long)p1
);
755 domain
->pt_root
= NULL
;
759 * Free a domain, only used if something went wrong in the
760 * allocation path and we need to free an already allocated page table
762 static void dma_ops_domain_free(struct dma_ops_domain
*dom
)
767 free_pagetable(&dom
->domain
);
769 kfree(dom
->pte_pages
);
777 * Allocates a new protection domain usable for the dma_ops functions.
778 * It also intializes the page table and the address allocator data
779 * structures required for the dma_ops interface
781 static struct dma_ops_domain
*dma_ops_domain_alloc(struct amd_iommu
*iommu
,
784 struct dma_ops_domain
*dma_dom
;
785 unsigned i
, num_pte_pages
;
790 * Currently the DMA aperture must be between 32 MB and 1GB in size
792 if ((order
< 25) || (order
> 30))
795 dma_dom
= kzalloc(sizeof(struct dma_ops_domain
), GFP_KERNEL
);
799 spin_lock_init(&dma_dom
->domain
.lock
);
801 dma_dom
->domain
.id
= domain_id_alloc();
802 if (dma_dom
->domain
.id
== 0)
804 dma_dom
->domain
.mode
= PAGE_MODE_3_LEVEL
;
805 dma_dom
->domain
.pt_root
= (void *)get_zeroed_page(GFP_KERNEL
);
806 dma_dom
->domain
.flags
= PD_DMA_OPS_MASK
;
807 dma_dom
->domain
.priv
= dma_dom
;
808 if (!dma_dom
->domain
.pt_root
)
810 dma_dom
->aperture_size
= (1ULL << order
);
811 dma_dom
->bitmap
= kzalloc(dma_dom
->aperture_size
/ (PAGE_SIZE
* 8),
813 if (!dma_dom
->bitmap
)
816 * mark the first page as allocated so we never return 0 as
817 * a valid dma-address. So we can use 0 as error value
819 dma_dom
->bitmap
[0] = 1;
820 dma_dom
->next_bit
= 0;
822 dma_dom
->need_flush
= false;
823 dma_dom
->target_dev
= 0xffff;
825 /* Intialize the exclusion range if necessary */
826 if (iommu
->exclusion_start
&&
827 iommu
->exclusion_start
< dma_dom
->aperture_size
) {
828 unsigned long startpage
= iommu
->exclusion_start
>> PAGE_SHIFT
;
829 int pages
= iommu_num_pages(iommu
->exclusion_start
,
830 iommu
->exclusion_length
,
832 dma_ops_reserve_addresses(dma_dom
, startpage
, pages
);
836 * At the last step, build the page tables so we don't need to
837 * allocate page table pages in the dma_ops mapping/unmapping
840 num_pte_pages
= dma_dom
->aperture_size
/ (PAGE_SIZE
* 512);
841 dma_dom
->pte_pages
= kzalloc(num_pte_pages
* sizeof(void *),
843 if (!dma_dom
->pte_pages
)
846 l2_pde
= (u64
*)get_zeroed_page(GFP_KERNEL
);
850 dma_dom
->domain
.pt_root
[0] = IOMMU_L2_PDE(virt_to_phys(l2_pde
));
852 for (i
= 0; i
< num_pte_pages
; ++i
) {
853 dma_dom
->pte_pages
[i
] = (u64
*)get_zeroed_page(GFP_KERNEL
);
854 if (!dma_dom
->pte_pages
[i
])
856 address
= virt_to_phys(dma_dom
->pte_pages
[i
]);
857 l2_pde
[i
] = IOMMU_L1_PDE(address
);
863 dma_ops_domain_free(dma_dom
);
869 * little helper function to check whether a given protection domain is a
872 static bool dma_ops_domain(struct protection_domain
*domain
)
874 return domain
->flags
& PD_DMA_OPS_MASK
;
878 * Find out the protection domain structure for a given PCI device. This
879 * will give us the pointer to the page table root for example.
881 static struct protection_domain
*domain_for_device(u16 devid
)
883 struct protection_domain
*dom
;
886 read_lock_irqsave(&amd_iommu_devtable_lock
, flags
);
887 dom
= amd_iommu_pd_table
[devid
];
888 read_unlock_irqrestore(&amd_iommu_devtable_lock
, flags
);
894 * If a device is not yet associated with a domain, this function does
895 * assigns it visible for the hardware
897 static void attach_device(struct amd_iommu
*iommu
,
898 struct protection_domain
*domain
,
902 u64 pte_root
= virt_to_phys(domain
->pt_root
);
904 domain
->dev_cnt
+= 1;
906 pte_root
|= (domain
->mode
& DEV_ENTRY_MODE_MASK
)
907 << DEV_ENTRY_MODE_SHIFT
;
908 pte_root
|= IOMMU_PTE_IR
| IOMMU_PTE_IW
| IOMMU_PTE_P
| IOMMU_PTE_TV
;
910 write_lock_irqsave(&amd_iommu_devtable_lock
, flags
);
911 amd_iommu_dev_table
[devid
].data
[0] = lower_32_bits(pte_root
);
912 amd_iommu_dev_table
[devid
].data
[1] = upper_32_bits(pte_root
);
913 amd_iommu_dev_table
[devid
].data
[2] = domain
->id
;
915 amd_iommu_pd_table
[devid
] = domain
;
916 write_unlock_irqrestore(&amd_iommu_devtable_lock
, flags
);
918 iommu_queue_inv_dev_entry(iommu
, devid
);
922 * Removes a device from a protection domain (unlocked)
924 static void __detach_device(struct protection_domain
*domain
, u16 devid
)
928 spin_lock(&domain
->lock
);
930 /* remove domain from the lookup table */
931 amd_iommu_pd_table
[devid
] = NULL
;
933 /* remove entry from the device table seen by the hardware */
934 amd_iommu_dev_table
[devid
].data
[0] = IOMMU_PTE_P
| IOMMU_PTE_TV
;
935 amd_iommu_dev_table
[devid
].data
[1] = 0;
936 amd_iommu_dev_table
[devid
].data
[2] = 0;
938 /* decrease reference counter */
939 domain
->dev_cnt
-= 1;
942 spin_unlock(&domain
->lock
);
946 * Removes a device from a protection domain (with devtable_lock held)
948 static void detach_device(struct protection_domain
*domain
, u16 devid
)
952 /* lock device table */
953 write_lock_irqsave(&amd_iommu_devtable_lock
, flags
);
954 __detach_device(domain
, devid
);
955 write_unlock_irqrestore(&amd_iommu_devtable_lock
, flags
);
958 static int device_change_notifier(struct notifier_block
*nb
,
959 unsigned long action
, void *data
)
961 struct device
*dev
= data
;
962 struct pci_dev
*pdev
= to_pci_dev(dev
);
963 u16 devid
= calc_devid(pdev
->bus
->number
, pdev
->devfn
);
964 struct protection_domain
*domain
;
965 struct dma_ops_domain
*dma_domain
;
966 struct amd_iommu
*iommu
;
967 int order
= amd_iommu_aperture_order
;
970 if (devid
> amd_iommu_last_bdf
)
973 devid
= amd_iommu_alias_table
[devid
];
975 iommu
= amd_iommu_rlookup_table
[devid
];
979 domain
= domain_for_device(devid
);
981 if (domain
&& !dma_ops_domain(domain
))
982 WARN_ONCE(1, "AMD IOMMU WARNING: device %s already bound "
983 "to a non-dma-ops domain\n", dev_name(dev
));
986 case BUS_NOTIFY_BOUND_DRIVER
:
989 dma_domain
= find_protection_domain(devid
);
991 dma_domain
= iommu
->default_dom
;
992 attach_device(iommu
, &dma_domain
->domain
, devid
);
993 printk(KERN_INFO
"AMD IOMMU: Using protection domain %d for "
994 "device %s\n", dma_domain
->domain
.id
, dev_name(dev
));
996 case BUS_NOTIFY_UNBIND_DRIVER
:
999 detach_device(domain
, devid
);
1001 case BUS_NOTIFY_ADD_DEVICE
:
1002 /* allocate a protection domain if a device is added */
1003 dma_domain
= find_protection_domain(devid
);
1006 dma_domain
= dma_ops_domain_alloc(iommu
, order
);
1009 dma_domain
->target_dev
= devid
;
1011 spin_lock_irqsave(&iommu_pd_list_lock
, flags
);
1012 list_add_tail(&dma_domain
->list
, &iommu_pd_list
);
1013 spin_unlock_irqrestore(&iommu_pd_list_lock
, flags
);
1020 iommu_queue_inv_dev_entry(iommu
, devid
);
1021 iommu_completion_wait(iommu
);
1027 struct notifier_block device_nb
= {
1028 .notifier_call
= device_change_notifier
,
1031 /*****************************************************************************
1033 * The next functions belong to the dma_ops mapping/unmapping code.
1035 *****************************************************************************/
1038 * This function checks if the driver got a valid device from the caller to
1039 * avoid dereferencing invalid pointers.
1041 static bool check_device(struct device
*dev
)
1043 if (!dev
|| !dev
->dma_mask
)
1050 * In this function the list of preallocated protection domains is traversed to
1051 * find the domain for a specific device
1053 static struct dma_ops_domain
*find_protection_domain(u16 devid
)
1055 struct dma_ops_domain
*entry
, *ret
= NULL
;
1056 unsigned long flags
;
1058 if (list_empty(&iommu_pd_list
))
1061 spin_lock_irqsave(&iommu_pd_list_lock
, flags
);
1063 list_for_each_entry(entry
, &iommu_pd_list
, list
) {
1064 if (entry
->target_dev
== devid
) {
1070 spin_unlock_irqrestore(&iommu_pd_list_lock
, flags
);
1076 * In the dma_ops path we only have the struct device. This function
1077 * finds the corresponding IOMMU, the protection domain and the
1078 * requestor id for a given device.
1079 * If the device is not yet associated with a domain this is also done
1082 static int get_device_resources(struct device
*dev
,
1083 struct amd_iommu
**iommu
,
1084 struct protection_domain
**domain
,
1087 struct dma_ops_domain
*dma_dom
;
1088 struct pci_dev
*pcidev
;
1095 if (dev
->bus
!= &pci_bus_type
)
1098 pcidev
= to_pci_dev(dev
);
1099 _bdf
= calc_devid(pcidev
->bus
->number
, pcidev
->devfn
);
1101 /* device not translated by any IOMMU in the system? */
1102 if (_bdf
> amd_iommu_last_bdf
)
1105 *bdf
= amd_iommu_alias_table
[_bdf
];
1107 *iommu
= amd_iommu_rlookup_table
[*bdf
];
1110 *domain
= domain_for_device(*bdf
);
1111 if (*domain
== NULL
) {
1112 dma_dom
= find_protection_domain(*bdf
);
1114 dma_dom
= (*iommu
)->default_dom
;
1115 *domain
= &dma_dom
->domain
;
1116 attach_device(*iommu
, *domain
, *bdf
);
1117 printk(KERN_INFO
"AMD IOMMU: Using protection domain %d for "
1118 "device %s\n", (*domain
)->id
, dev_name(dev
));
1121 if (domain_for_device(_bdf
) == NULL
)
1122 attach_device(*iommu
, *domain
, _bdf
);
1128 * This is the generic map function. It maps one 4kb page at paddr to
1129 * the given address in the DMA address space for the domain.
1131 static dma_addr_t
dma_ops_domain_map(struct amd_iommu
*iommu
,
1132 struct dma_ops_domain
*dom
,
1133 unsigned long address
,
1139 WARN_ON(address
> dom
->aperture_size
);
1143 pte
= dom
->pte_pages
[IOMMU_PTE_L1_INDEX(address
)];
1144 pte
+= IOMMU_PTE_L0_INDEX(address
);
1146 __pte
= paddr
| IOMMU_PTE_P
| IOMMU_PTE_FC
;
1148 if (direction
== DMA_TO_DEVICE
)
1149 __pte
|= IOMMU_PTE_IR
;
1150 else if (direction
== DMA_FROM_DEVICE
)
1151 __pte
|= IOMMU_PTE_IW
;
1152 else if (direction
== DMA_BIDIRECTIONAL
)
1153 __pte
|= IOMMU_PTE_IR
| IOMMU_PTE_IW
;
1159 return (dma_addr_t
)address
;
1163 * The generic unmapping function for on page in the DMA address space.
1165 static void dma_ops_domain_unmap(struct amd_iommu
*iommu
,
1166 struct dma_ops_domain
*dom
,
1167 unsigned long address
)
1171 if (address
>= dom
->aperture_size
)
1174 WARN_ON(address
& ~PAGE_MASK
|| address
>= dom
->aperture_size
);
1176 pte
= dom
->pte_pages
[IOMMU_PTE_L1_INDEX(address
)];
1177 pte
+= IOMMU_PTE_L0_INDEX(address
);
1185 * This function contains common code for mapping of a physically
1186 * contiguous memory region into DMA address space. It is used by all
1187 * mapping functions provided with this IOMMU driver.
1188 * Must be called with the domain lock held.
1190 static dma_addr_t
__map_single(struct device
*dev
,
1191 struct amd_iommu
*iommu
,
1192 struct dma_ops_domain
*dma_dom
,
1199 dma_addr_t offset
= paddr
& ~PAGE_MASK
;
1200 dma_addr_t address
, start
;
1202 unsigned long align_mask
= 0;
1205 pages
= iommu_num_pages(paddr
, size
, PAGE_SIZE
);
1209 align_mask
= (1UL << get_order(size
)) - 1;
1211 address
= dma_ops_alloc_addresses(dev
, dma_dom
, pages
, align_mask
,
1213 if (unlikely(address
== bad_dma_address
))
1217 for (i
= 0; i
< pages
; ++i
) {
1218 dma_ops_domain_map(iommu
, dma_dom
, start
, paddr
, dir
);
1224 if (unlikely(dma_dom
->need_flush
&& !amd_iommu_unmap_flush
)) {
1225 iommu_flush_tlb(iommu
, dma_dom
->domain
.id
);
1226 dma_dom
->need_flush
= false;
1227 } else if (unlikely(iommu_has_npcache(iommu
)))
1228 iommu_flush_pages(iommu
, dma_dom
->domain
.id
, address
, size
);
1235 * Does the reverse of the __map_single function. Must be called with
1236 * the domain lock held too
1238 static void __unmap_single(struct amd_iommu
*iommu
,
1239 struct dma_ops_domain
*dma_dom
,
1240 dma_addr_t dma_addr
,
1244 dma_addr_t i
, start
;
1247 if ((dma_addr
== bad_dma_address
) ||
1248 (dma_addr
+ size
> dma_dom
->aperture_size
))
1251 pages
= iommu_num_pages(dma_addr
, size
, PAGE_SIZE
);
1252 dma_addr
&= PAGE_MASK
;
1255 for (i
= 0; i
< pages
; ++i
) {
1256 dma_ops_domain_unmap(iommu
, dma_dom
, start
);
1260 dma_ops_free_addresses(dma_dom
, dma_addr
, pages
);
1262 if (amd_iommu_unmap_flush
|| dma_dom
->need_flush
) {
1263 iommu_flush_pages(iommu
, dma_dom
->domain
.id
, dma_addr
, size
);
1264 dma_dom
->need_flush
= false;
1269 * The exported map_single function for dma_ops.
1271 static dma_addr_t
map_single(struct device
*dev
, phys_addr_t paddr
,
1272 size_t size
, int dir
)
1274 unsigned long flags
;
1275 struct amd_iommu
*iommu
;
1276 struct protection_domain
*domain
;
1281 if (!check_device(dev
))
1282 return bad_dma_address
;
1284 dma_mask
= *dev
->dma_mask
;
1286 get_device_resources(dev
, &iommu
, &domain
, &devid
);
1288 if (iommu
== NULL
|| domain
== NULL
)
1289 /* device not handled by any AMD IOMMU */
1290 return (dma_addr_t
)paddr
;
1292 if (!dma_ops_domain(domain
))
1293 return bad_dma_address
;
1295 spin_lock_irqsave(&domain
->lock
, flags
);
1296 addr
= __map_single(dev
, iommu
, domain
->priv
, paddr
, size
, dir
, false,
1298 if (addr
== bad_dma_address
)
1301 iommu_completion_wait(iommu
);
1304 spin_unlock_irqrestore(&domain
->lock
, flags
);
1310 * The exported unmap_single function for dma_ops.
1312 static void unmap_single(struct device
*dev
, dma_addr_t dma_addr
,
1313 size_t size
, int dir
)
1315 unsigned long flags
;
1316 struct amd_iommu
*iommu
;
1317 struct protection_domain
*domain
;
1320 if (!check_device(dev
) ||
1321 !get_device_resources(dev
, &iommu
, &domain
, &devid
))
1322 /* device not handled by any AMD IOMMU */
1325 if (!dma_ops_domain(domain
))
1328 spin_lock_irqsave(&domain
->lock
, flags
);
1330 __unmap_single(iommu
, domain
->priv
, dma_addr
, size
, dir
);
1332 iommu_completion_wait(iommu
);
1334 spin_unlock_irqrestore(&domain
->lock
, flags
);
1338 * This is a special map_sg function which is used if we should map a
1339 * device which is not handled by an AMD IOMMU in the system.
1341 static int map_sg_no_iommu(struct device
*dev
, struct scatterlist
*sglist
,
1342 int nelems
, int dir
)
1344 struct scatterlist
*s
;
1347 for_each_sg(sglist
, s
, nelems
, i
) {
1348 s
->dma_address
= (dma_addr_t
)sg_phys(s
);
1349 s
->dma_length
= s
->length
;
1356 * The exported map_sg function for dma_ops (handles scatter-gather
1359 static int map_sg(struct device
*dev
, struct scatterlist
*sglist
,
1360 int nelems
, int dir
)
1362 unsigned long flags
;
1363 struct amd_iommu
*iommu
;
1364 struct protection_domain
*domain
;
1367 struct scatterlist
*s
;
1369 int mapped_elems
= 0;
1372 if (!check_device(dev
))
1375 dma_mask
= *dev
->dma_mask
;
1377 get_device_resources(dev
, &iommu
, &domain
, &devid
);
1379 if (!iommu
|| !domain
)
1380 return map_sg_no_iommu(dev
, sglist
, nelems
, dir
);
1382 if (!dma_ops_domain(domain
))
1385 spin_lock_irqsave(&domain
->lock
, flags
);
1387 for_each_sg(sglist
, s
, nelems
, i
) {
1390 s
->dma_address
= __map_single(dev
, iommu
, domain
->priv
,
1391 paddr
, s
->length
, dir
, false,
1394 if (s
->dma_address
) {
1395 s
->dma_length
= s
->length
;
1401 iommu_completion_wait(iommu
);
1404 spin_unlock_irqrestore(&domain
->lock
, flags
);
1406 return mapped_elems
;
1408 for_each_sg(sglist
, s
, mapped_elems
, i
) {
1410 __unmap_single(iommu
, domain
->priv
, s
->dma_address
,
1411 s
->dma_length
, dir
);
1412 s
->dma_address
= s
->dma_length
= 0;
1421 * The exported map_sg function for dma_ops (handles scatter-gather
1424 static void unmap_sg(struct device
*dev
, struct scatterlist
*sglist
,
1425 int nelems
, int dir
)
1427 unsigned long flags
;
1428 struct amd_iommu
*iommu
;
1429 struct protection_domain
*domain
;
1430 struct scatterlist
*s
;
1434 if (!check_device(dev
) ||
1435 !get_device_resources(dev
, &iommu
, &domain
, &devid
))
1438 if (!dma_ops_domain(domain
))
1441 spin_lock_irqsave(&domain
->lock
, flags
);
1443 for_each_sg(sglist
, s
, nelems
, i
) {
1444 __unmap_single(iommu
, domain
->priv
, s
->dma_address
,
1445 s
->dma_length
, dir
);
1446 s
->dma_address
= s
->dma_length
= 0;
1449 iommu_completion_wait(iommu
);
1451 spin_unlock_irqrestore(&domain
->lock
, flags
);
1455 * The exported alloc_coherent function for dma_ops.
1457 static void *alloc_coherent(struct device
*dev
, size_t size
,
1458 dma_addr_t
*dma_addr
, gfp_t flag
)
1460 unsigned long flags
;
1462 struct amd_iommu
*iommu
;
1463 struct protection_domain
*domain
;
1466 u64 dma_mask
= dev
->coherent_dma_mask
;
1468 if (!check_device(dev
))
1471 if (!get_device_resources(dev
, &iommu
, &domain
, &devid
))
1472 flag
&= ~(__GFP_DMA
| __GFP_HIGHMEM
| __GFP_DMA32
);
1475 virt_addr
= (void *)__get_free_pages(flag
, get_order(size
));
1479 paddr
= virt_to_phys(virt_addr
);
1481 if (!iommu
|| !domain
) {
1482 *dma_addr
= (dma_addr_t
)paddr
;
1486 if (!dma_ops_domain(domain
))
1490 dma_mask
= *dev
->dma_mask
;
1492 spin_lock_irqsave(&domain
->lock
, flags
);
1494 *dma_addr
= __map_single(dev
, iommu
, domain
->priv
, paddr
,
1495 size
, DMA_BIDIRECTIONAL
, true, dma_mask
);
1497 if (*dma_addr
== bad_dma_address
)
1500 iommu_completion_wait(iommu
);
1502 spin_unlock_irqrestore(&domain
->lock
, flags
);
1508 free_pages((unsigned long)virt_addr
, get_order(size
));
1514 * The exported free_coherent function for dma_ops.
1516 static void free_coherent(struct device
*dev
, size_t size
,
1517 void *virt_addr
, dma_addr_t dma_addr
)
1519 unsigned long flags
;
1520 struct amd_iommu
*iommu
;
1521 struct protection_domain
*domain
;
1524 if (!check_device(dev
))
1527 get_device_resources(dev
, &iommu
, &domain
, &devid
);
1529 if (!iommu
|| !domain
)
1532 if (!dma_ops_domain(domain
))
1535 spin_lock_irqsave(&domain
->lock
, flags
);
1537 __unmap_single(iommu
, domain
->priv
, dma_addr
, size
, DMA_BIDIRECTIONAL
);
1539 iommu_completion_wait(iommu
);
1541 spin_unlock_irqrestore(&domain
->lock
, flags
);
1544 free_pages((unsigned long)virt_addr
, get_order(size
));
1548 * This function is called by the DMA layer to find out if we can handle a
1549 * particular device. It is part of the dma_ops.
1551 static int amd_iommu_dma_supported(struct device
*dev
, u64 mask
)
1554 struct pci_dev
*pcidev
;
1556 /* No device or no PCI device */
1557 if (!dev
|| dev
->bus
!= &pci_bus_type
)
1560 pcidev
= to_pci_dev(dev
);
1562 bdf
= calc_devid(pcidev
->bus
->number
, pcidev
->devfn
);
1564 /* Out of our scope? */
1565 if (bdf
> amd_iommu_last_bdf
)
1572 * The function for pre-allocating protection domains.
1574 * If the driver core informs the DMA layer if a driver grabs a device
1575 * we don't need to preallocate the protection domains anymore.
1576 * For now we have to.
1578 void prealloc_protection_domains(void)
1580 struct pci_dev
*dev
= NULL
;
1581 struct dma_ops_domain
*dma_dom
;
1582 struct amd_iommu
*iommu
;
1583 int order
= amd_iommu_aperture_order
;
1586 while ((dev
= pci_get_device(PCI_ANY_ID
, PCI_ANY_ID
, dev
)) != NULL
) {
1587 devid
= calc_devid(dev
->bus
->number
, dev
->devfn
);
1588 if (devid
> amd_iommu_last_bdf
)
1590 devid
= amd_iommu_alias_table
[devid
];
1591 if (domain_for_device(devid
))
1593 iommu
= amd_iommu_rlookup_table
[devid
];
1596 dma_dom
= dma_ops_domain_alloc(iommu
, order
);
1599 init_unity_mappings_for_device(dma_dom
, devid
);
1600 dma_dom
->target_dev
= devid
;
1602 list_add_tail(&dma_dom
->list
, &iommu_pd_list
);
1606 static struct dma_mapping_ops amd_iommu_dma_ops
= {
1607 .alloc_coherent
= alloc_coherent
,
1608 .free_coherent
= free_coherent
,
1609 .map_single
= map_single
,
1610 .unmap_single
= unmap_single
,
1612 .unmap_sg
= unmap_sg
,
1613 .dma_supported
= amd_iommu_dma_supported
,
1617 * The function which clues the AMD IOMMU driver into dma_ops.
1619 int __init
amd_iommu_init_dma_ops(void)
1621 struct amd_iommu
*iommu
;
1622 int order
= amd_iommu_aperture_order
;
1626 * first allocate a default protection domain for every IOMMU we
1627 * found in the system. Devices not assigned to any other
1628 * protection domain will be assigned to the default one.
1630 list_for_each_entry(iommu
, &amd_iommu_list
, list
) {
1631 iommu
->default_dom
= dma_ops_domain_alloc(iommu
, order
);
1632 if (iommu
->default_dom
== NULL
)
1634 iommu
->default_dom
->domain
.flags
|= PD_DEFAULT_MASK
;
1635 ret
= iommu_init_unity_mappings(iommu
);
1641 * If device isolation is enabled, pre-allocate the protection
1642 * domains for each device.
1644 if (amd_iommu_isolate
)
1645 prealloc_protection_domains();
1649 bad_dma_address
= 0;
1650 #ifdef CONFIG_GART_IOMMU
1651 gart_iommu_aperture_disabled
= 1;
1652 gart_iommu_aperture
= 0;
1655 /* Make the driver finally visible to the drivers */
1656 dma_ops
= &amd_iommu_dma_ops
;
1658 #ifdef CONFIG_IOMMU_API
1659 register_iommu(&amd_iommu_ops
);
1662 bus_register_notifier(&pci_bus_type
, &device_nb
);
1664 amd_iommu_stats_init();
1670 list_for_each_entry(iommu
, &amd_iommu_list
, list
) {
1671 if (iommu
->default_dom
)
1672 dma_ops_domain_free(iommu
->default_dom
);
1678 /*****************************************************************************
1680 * The following functions belong to the exported interface of AMD IOMMU
1682 * This interface allows access to lower level functions of the IOMMU
1683 * like protection domain handling and assignement of devices to domains
1684 * which is not possible with the dma_ops interface.
1686 *****************************************************************************/
1688 #ifdef CONFIG_IOMMU_API
1690 static void cleanup_domain(struct protection_domain
*domain
)
1692 unsigned long flags
;
1695 write_lock_irqsave(&amd_iommu_devtable_lock
, flags
);
1697 for (devid
= 0; devid
<= amd_iommu_last_bdf
; ++devid
)
1698 if (amd_iommu_pd_table
[devid
] == domain
)
1699 __detach_device(domain
, devid
);
1701 write_unlock_irqrestore(&amd_iommu_devtable_lock
, flags
);
1704 static int amd_iommu_domain_init(struct iommu_domain
*dom
)
1706 struct protection_domain
*domain
;
1708 domain
= kzalloc(sizeof(*domain
), GFP_KERNEL
);
1712 spin_lock_init(&domain
->lock
);
1713 domain
->mode
= PAGE_MODE_3_LEVEL
;
1714 domain
->id
= domain_id_alloc();
1717 domain
->pt_root
= (void *)get_zeroed_page(GFP_KERNEL
);
1718 if (!domain
->pt_root
)
1731 static void amd_iommu_domain_destroy(struct iommu_domain
*dom
)
1733 struct protection_domain
*domain
= dom
->priv
;
1738 if (domain
->dev_cnt
> 0)
1739 cleanup_domain(domain
);
1741 BUG_ON(domain
->dev_cnt
!= 0);
1743 free_pagetable(domain
);
1745 domain_id_free(domain
->id
);
1752 static void amd_iommu_detach_device(struct iommu_domain
*dom
,
1755 struct protection_domain
*domain
= dom
->priv
;
1756 struct amd_iommu
*iommu
;
1757 struct pci_dev
*pdev
;
1760 if (dev
->bus
!= &pci_bus_type
)
1763 pdev
= to_pci_dev(dev
);
1765 devid
= calc_devid(pdev
->bus
->number
, pdev
->devfn
);
1768 detach_device(domain
, devid
);
1770 iommu
= amd_iommu_rlookup_table
[devid
];
1774 iommu_queue_inv_dev_entry(iommu
, devid
);
1775 iommu_completion_wait(iommu
);
1778 static int amd_iommu_attach_device(struct iommu_domain
*dom
,
1781 struct protection_domain
*domain
= dom
->priv
;
1782 struct protection_domain
*old_domain
;
1783 struct amd_iommu
*iommu
;
1784 struct pci_dev
*pdev
;
1787 if (dev
->bus
!= &pci_bus_type
)
1790 pdev
= to_pci_dev(dev
);
1792 devid
= calc_devid(pdev
->bus
->number
, pdev
->devfn
);
1794 if (devid
>= amd_iommu_last_bdf
||
1795 devid
!= amd_iommu_alias_table
[devid
])
1798 iommu
= amd_iommu_rlookup_table
[devid
];
1802 old_domain
= domain_for_device(devid
);
1806 attach_device(iommu
, domain
, devid
);
1808 iommu_completion_wait(iommu
);
1813 static int amd_iommu_map_range(struct iommu_domain
*dom
,
1814 unsigned long iova
, phys_addr_t paddr
,
1815 size_t size
, int iommu_prot
)
1817 struct protection_domain
*domain
= dom
->priv
;
1818 unsigned long i
, npages
= iommu_num_pages(paddr
, size
, PAGE_SIZE
);
1822 if (iommu_prot
& IOMMU_READ
)
1823 prot
|= IOMMU_PROT_IR
;
1824 if (iommu_prot
& IOMMU_WRITE
)
1825 prot
|= IOMMU_PROT_IW
;
1830 for (i
= 0; i
< npages
; ++i
) {
1831 ret
= iommu_map_page(domain
, iova
, paddr
, prot
);
1842 static void amd_iommu_unmap_range(struct iommu_domain
*dom
,
1843 unsigned long iova
, size_t size
)
1846 struct protection_domain
*domain
= dom
->priv
;
1847 unsigned long i
, npages
= iommu_num_pages(iova
, size
, PAGE_SIZE
);
1851 for (i
= 0; i
< npages
; ++i
) {
1852 iommu_unmap_page(domain
, iova
);
1856 iommu_flush_domain(domain
->id
);
1859 static phys_addr_t
amd_iommu_iova_to_phys(struct iommu_domain
*dom
,
1862 struct protection_domain
*domain
= dom
->priv
;
1863 unsigned long offset
= iova
& ~PAGE_MASK
;
1867 pte
= &domain
->pt_root
[IOMMU_PTE_L2_INDEX(iova
)];
1869 if (!IOMMU_PTE_PRESENT(*pte
))
1872 pte
= IOMMU_PTE_PAGE(*pte
);
1873 pte
= &pte
[IOMMU_PTE_L1_INDEX(iova
)];
1875 if (!IOMMU_PTE_PRESENT(*pte
))
1878 pte
= IOMMU_PTE_PAGE(*pte
);
1879 pte
= &pte
[IOMMU_PTE_L0_INDEX(iova
)];
1881 if (!IOMMU_PTE_PRESENT(*pte
))
1884 paddr
= *pte
& IOMMU_PAGE_MASK
;
1890 static struct iommu_ops amd_iommu_ops
= {
1891 .domain_init
= amd_iommu_domain_init
,
1892 .domain_destroy
= amd_iommu_domain_destroy
,
1893 .attach_dev
= amd_iommu_attach_device
,
1894 .detach_dev
= amd_iommu_detach_device
,
1895 .map
= amd_iommu_map_range
,
1896 .unmap
= amd_iommu_unmap_range
,
1897 .iova_to_phys
= amd_iommu_iova_to_phys
,