Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
| 2 | #ifndef __LINUX_SWIOTLB_H |
| 3 | #define __LINUX_SWIOTLB_H |
| 4 | |
| 5 | #include <linux/dma-direction.h> |
| 6 | #include <linux/init.h> |
| 7 | #include <linux/types.h> |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 8 | #include <linux/limits.h> |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9 | |
| 10 | struct device; |
| 11 | struct page; |
| 12 | struct scatterlist; |
| 13 | |
| 14 | enum swiotlb_force { |
| 15 | SWIOTLB_NORMAL, /* Default - depending on HW DMA mask etc. */ |
| 16 | SWIOTLB_FORCE, /* swiotlb=force */ |
| 17 | SWIOTLB_NO_FORCE, /* swiotlb=noforce */ |
| 18 | }; |
| 19 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 20 | /* |
| 21 | * Maximum allowable number of contiguous slabs to map, |
| 22 | * must be a power of 2. What is the appropriate value ? |
| 23 | * The complexity of {map,unmap}_single is linearly dependent on this value. |
| 24 | */ |
| 25 | #define IO_TLB_SEGSIZE 128 |
| 26 | |
| 27 | /* |
| 28 | * log of the size of each IO TLB slab. The number of slabs is command line |
| 29 | * controllable. |
| 30 | */ |
| 31 | #define IO_TLB_SHIFT 11 |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 32 | #define IO_TLB_SIZE (1 << IO_TLB_SHIFT) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 33 | |
| 34 | extern void swiotlb_init(int verbose); |
| 35 | int swiotlb_init_with_tbl(char *tlb, unsigned long nslabs, int verbose); |
| 36 | extern unsigned long swiotlb_nr_tbl(void); |
| 37 | unsigned long swiotlb_size_or_default(void); |
| 38 | extern int swiotlb_late_init_with_tbl(char *tlb, unsigned long nslabs); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 39 | extern int swiotlb_late_init_with_default_size(size_t default_size); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 40 | extern void __init swiotlb_update_mem_attributes(void); |
| 41 | |
| 42 | /* |
| 43 | * Enumeration for sync targets |
| 44 | */ |
| 45 | enum dma_sync_target { |
| 46 | SYNC_FOR_CPU = 0, |
| 47 | SYNC_FOR_DEVICE = 1, |
| 48 | }; |
| 49 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 50 | phys_addr_t swiotlb_tbl_map_single(struct device *hwdev, phys_addr_t phys, |
| 51 | size_t mapping_size, size_t alloc_size, |
| 52 | enum dma_data_direction dir, unsigned long attrs); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 53 | |
| 54 | extern void swiotlb_tbl_unmap_single(struct device *hwdev, |
| 55 | phys_addr_t tlb_addr, |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 56 | size_t mapping_size, |
| 57 | size_t alloc_size, |
| 58 | enum dma_data_direction dir, |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 59 | unsigned long attrs); |
| 60 | |
| 61 | extern void swiotlb_tbl_sync_single(struct device *hwdev, |
| 62 | phys_addr_t tlb_addr, |
| 63 | size_t size, enum dma_data_direction dir, |
| 64 | enum dma_sync_target target); |
| 65 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 66 | dma_addr_t swiotlb_map(struct device *dev, phys_addr_t phys, |
| 67 | size_t size, enum dma_data_direction dir, unsigned long attrs); |
| 68 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 69 | #ifdef CONFIG_SWIOTLB |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 70 | extern enum swiotlb_force swiotlb_force; |
| 71 | extern phys_addr_t io_tlb_start, io_tlb_end; |
| 72 | |
| 73 | static inline bool is_swiotlb_buffer(phys_addr_t paddr) |
| 74 | { |
| 75 | return paddr >= io_tlb_start && paddr < io_tlb_end; |
| 76 | } |
| 77 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 78 | void __init swiotlb_exit(void); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 79 | unsigned int swiotlb_max_segment(void); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 80 | size_t swiotlb_max_mapping_size(struct device *dev); |
| 81 | bool is_swiotlb_active(void); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 82 | #else |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 83 | #define swiotlb_force SWIOTLB_NO_FORCE |
| 84 | static inline bool is_swiotlb_buffer(phys_addr_t paddr) |
| 85 | { |
| 86 | return false; |
| 87 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 88 | static inline void swiotlb_exit(void) |
| 89 | { |
| 90 | } |
| 91 | static inline unsigned int swiotlb_max_segment(void) |
| 92 | { |
| 93 | return 0; |
| 94 | } |
| 95 | static inline size_t swiotlb_max_mapping_size(struct device *dev) |
| 96 | { |
| 97 | return SIZE_MAX; |
| 98 | } |
| 99 | |
| 100 | static inline bool is_swiotlb_active(void) |
| 101 | { |
| 102 | return false; |
| 103 | } |
| 104 | #endif /* CONFIG_SWIOTLB */ |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 105 | |
| 106 | extern void swiotlb_print_info(void); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 107 | extern void swiotlb_set_max_segment(unsigned int); |
| 108 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 109 | #endif /* __LINUX_SWIOTLB_H */ |