1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
21e19b16aSThomas Gleixner #ifndef _LINUX_IOMMU_HELPER_H
31e19b16aSThomas Gleixner #define _LINUX_IOMMU_HELPER_H
41e19b16aSThomas Gleixner
579c1879eSChristoph Hellwig #include <linux/bug.h>
6*aa6159abSAndy Shevchenko #include <linux/log2.h>
7*aa6159abSAndy Shevchenko #include <linux/math.h>
8*aa6159abSAndy Shevchenko #include <linux/types.h>
9e269b085SAnton Blanchard
iommu_device_max_index(unsigned long size,unsigned long offset,u64 dma_mask)10eecfffc1SFUJITA Tomonori static inline unsigned long iommu_device_max_index(unsigned long size,
11eecfffc1SFUJITA Tomonori unsigned long offset,
12eecfffc1SFUJITA Tomonori u64 dma_mask)
13eecfffc1SFUJITA Tomonori {
14eecfffc1SFUJITA Tomonori if (size + offset > dma_mask)
15eecfffc1SFUJITA Tomonori return dma_mask - offset + 1;
16eecfffc1SFUJITA Tomonori else
17eecfffc1SFUJITA Tomonori return size;
18eecfffc1SFUJITA Tomonori }
19eecfffc1SFUJITA Tomonori
iommu_is_span_boundary(unsigned int index,unsigned int nr,unsigned long shift,unsigned long boundary_size)2079c1879eSChristoph Hellwig static inline int iommu_is_span_boundary(unsigned int index, unsigned int nr,
2179c1879eSChristoph Hellwig unsigned long shift, unsigned long boundary_size)
2279c1879eSChristoph Hellwig {
2379c1879eSChristoph Hellwig BUG_ON(!is_power_of_2(boundary_size));
2479c1879eSChristoph Hellwig
2579c1879eSChristoph Hellwig shift = (shift + index) & (boundary_size - 1);
2679c1879eSChristoph Hellwig return shift + nr > boundary_size;
2779c1879eSChristoph Hellwig }
2879c1879eSChristoph Hellwig
290291df8cSFUJITA Tomonori extern unsigned long iommu_area_alloc(unsigned long *map, unsigned long size,
300291df8cSFUJITA Tomonori unsigned long start, unsigned int nr,
310291df8cSFUJITA Tomonori unsigned long shift,
320291df8cSFUJITA Tomonori unsigned long boundary_size,
330291df8cSFUJITA Tomonori unsigned long align_mask);
341e19b16aSThomas Gleixner
iommu_num_pages(unsigned long addr,unsigned long len,unsigned long io_page_size)35e269b085SAnton Blanchard static inline unsigned long iommu_num_pages(unsigned long addr,
36e269b085SAnton Blanchard unsigned long len,
37e269b085SAnton Blanchard unsigned long io_page_size)
38e269b085SAnton Blanchard {
39e269b085SAnton Blanchard unsigned long size = (addr & (io_page_size - 1)) + len;
40e269b085SAnton Blanchard
41e269b085SAnton Blanchard return DIV_ROUND_UP(size, io_page_size);
42e269b085SAnton Blanchard }
4356d93660SJoerg Roedel
441e19b16aSThomas Gleixner #endif
45