1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Gasket Page Table functionality. This file describes the address
4 * translation/paging functionality supported by the Gasket driver framework.
5 * As much as possible, internal details are hidden to simplify use -
6 * all calls are thread-safe (protected by an internal mutex) except where
9 * Copyright (C) 2018 Google, Inc.
12 #ifndef __GASKET_PAGE_TABLE_H__
13 #define __GASKET_PAGE_TABLE_H__
15 #include <linux/pci.h>
16 #include <linux/types.h>
18 #include "gasket_constants.h"
19 #include "gasket_core.h"
22 * Structure used for managing address translation on a device. All details are
23 * internal to the implementation.
25 struct gasket_page_table;
28 * Allocate and init address translation data.
29 * @ppage_table: Pointer to Gasket page table pointer. Set by this call.
30 * @att_base_reg: [Mapped] pointer to the first entry in the device's address
32 * @extended_offset_reg: [Mapped] pointer to the device's register containing
33 * the starting index of the extended translation table.
34 * @extended_bit_location: The index of the bit indicating whether an address
36 * @total_entries: The total number of entries in the device's address
38 * @device: Device structure for the underlying device. Only used for logging.
39 * @pci_dev: PCI system descriptor for the underlying device.
40 * whether the driver will supply its own.
42 * Description: Allocates and initializes data to track address translation -
43 * simple and extended page table metadata. Initially, the page table is
44 * partitioned such that all addresses are "simple" (single-level lookup).
45 * gasket_partition_page_table can be called to change this paritioning.
47 * Returns 0 on success, a negative error code otherwise.
49 int gasket_page_table_init(struct gasket_page_table **ppg_tbl,
50 const struct gasket_bar_data *bar_data,
51 const struct gasket_page_table_config *page_table_config,
52 struct device *device, struct pci_dev *pci_dev);
55 * Deallocate and cleanup page table data.
56 * @page_table: Gasket page table pointer.
58 * Description: The inverse of gasket_init; frees page_table and its contained
61 * Because this call destroys the page table, it cannot be
62 * thread-safe (mutex-protected)!
64 void gasket_page_table_cleanup(struct gasket_page_table *page_table);
67 * Sets the size of the simple page table.
68 * @page_table: Gasket page table pointer.
69 * @num_simple_entries: Desired size of the simple page table (in entries).
71 * Description: gasket_partition_page_table checks to see if the simple page
72 * size can be changed (i.e., if there are no active extended
73 * mappings in the new simple size range), and, if so,
74 * sets the new simple and extended page table sizes.
76 * Returns 0 if successful, or non-zero if the page table entries
79 int gasket_page_table_partition(struct gasket_page_table *page_table,
80 uint num_simple_entries);
83 * Get and map [host] user space pages into device memory.
84 * @page_table: Gasket page table pointer.
85 * @host_addr: Starting host virtual memory address of the pages.
86 * @dev_addr: Starting device address of the pages.
87 * @num_pages: Number of [4kB] pages to map.
89 * Description: Maps the "num_pages" pages of host memory pointed to by
90 * host_addr to the address "dev_addr" in device memory.
92 * The caller is responsible for checking the addresses ranges.
94 * Returns 0 if successful or a non-zero error number otherwise.
95 * If there is an error, no pages are mapped.
97 int gasket_page_table_map(struct gasket_page_table *page_table, ulong host_addr,
98 ulong dev_addr, uint num_pages);
101 * Un-map host pages from device memory.
102 * @page_table: Gasket page table pointer.
103 * @dev_addr: Starting device address of the pages to unmap.
104 * @num_pages: The number of [4kB] pages to unmap.
106 * Description: The inverse of gasket_map_pages. Unmaps pages from the device.
108 void gasket_page_table_unmap(struct gasket_page_table *page_table,
109 ulong dev_addr, uint num_pages);
112 * Unmap ALL host pages from device memory.
113 * @page_table: Gasket page table pointer.
115 void gasket_page_table_unmap_all(struct gasket_page_table *page_table);
118 * Unmap all host pages from device memory and reset the table to fully simple
120 * @page_table: Gasket page table pointer.
122 void gasket_page_table_reset(struct gasket_page_table *page_table);
125 * Reclaims unused page table memory.
126 * @page_table: Gasket page table pointer.
128 * Description: Examines the page table and frees any currently-unused
129 * allocations. Called internally on gasket_cleanup().
131 void gasket_page_table_garbage_collect(struct gasket_page_table *page_table);
134 * Retrieve the backing page for a device address.
135 * @page_table: Gasket page table pointer.
136 * @dev_addr: Gasket device address.
137 * @ppage: Pointer to a page pointer for the returned page.
138 * @poffset: Pointer to an unsigned long for the returned offset.
140 * Description: Interprets the address and looks up the corresponding page
141 * in the page table and the offset in that page. (We need an
142 * offset because the host page may be larger than the Gasket chip
145 * Returns 0 if successful, -1 for an error. The page pointer
146 * and offset are returned through the pointers, if successful.
148 int gasket_page_table_lookup_page(struct gasket_page_table *page_table,
149 ulong dev_addr, struct page **page,
153 * Checks validity for input addrs and size.
154 * @page_table: Gasket page table pointer.
155 * @host_addr: Host address to check.
156 * @dev_addr: Gasket device address.
157 * @bytes: Size of the range to check (in bytes).
159 * Description: This call performs a number of checks to verify that the ranges
160 * specified by both addresses and the size are valid for mapping pages into
163 * Returns true if the mapping is bad, false otherwise.
165 bool gasket_page_table_are_addrs_bad(struct gasket_page_table *page_table,
166 ulong host_addr, ulong dev_addr,
170 * Checks validity for input dev addr and size.
171 * @page_table: Gasket page table pointer.
172 * @dev_addr: Gasket device address.
173 * @bytes: Size of the range to check (in bytes).
175 * Description: This call performs a number of checks to verify that the range
176 * specified by the device address and the size is valid for mapping pages into
179 * Returns true if the address is bad, false otherwise.
181 bool gasket_page_table_is_dev_addr_bad(struct gasket_page_table *page_table,
182 ulong dev_addr, ulong bytes);
185 * Gets maximum size for the given page table.
186 * @page_table: Gasket page table pointer.
188 uint gasket_page_table_max_size(struct gasket_page_table *page_table);
191 * Gets the total number of entries in the arg.
192 * @page_table: Gasket page table pointer.
194 uint gasket_page_table_num_entries(struct gasket_page_table *page_table);
197 * Gets the number of simple entries.
198 * @page_table: Gasket page table pointer.
200 uint gasket_page_table_num_simple_entries(struct gasket_page_table *page_table);
203 * Gets the number of actively pinned pages.
204 * @page_table: Gasket page table pointer.
206 uint gasket_page_table_num_active_pages(struct gasket_page_table *page_table);
209 * Get status of page table managed by @page_table.
210 * @page_table: Gasket page table pointer.
212 int gasket_page_table_system_status(struct gasket_page_table *page_table);
215 * Allocate a block of coherent memory.
216 * @gasket_dev: Gasket Device.
217 * @size: Size of the memory block.
218 * @dma_address: Dma address allocated by the kernel.
219 * @index: Index of the gasket_page_table within this Gasket device
221 * Description: Allocate a contiguous coherent memory block, DMA'ble
224 int gasket_alloc_coherent_memory(struct gasket_dev *gasket_dev, uint64_t size,
225 dma_addr_t *dma_address, uint64_t index);
226 /* Release a block of contiguous coherent memory, in use by a device. */
227 int gasket_free_coherent_memory(struct gasket_dev *gasket_dev, uint64_t size,
228 dma_addr_t dma_address, uint64_t index);
230 /* Release all coherent memory. */
231 void gasket_free_coherent_memory_all(struct gasket_dev *gasket_dev,
235 * Records the host_addr to coherent dma memory mapping.
236 * @gasket_dev: Gasket Device.
237 * @size: Size of the virtual address range to map.
238 * @dma_address: Dma address within the coherent memory range.
239 * @vma: Virtual address we wish to map to coherent memory.
241 * Description: For each page in the virtual address range, record the
242 * coherent page mapping.
244 * Does not perform validity checking.
246 int gasket_set_user_virt(struct gasket_dev *gasket_dev, uint64_t size,
247 dma_addr_t dma_address, ulong vma);
249 #endif /* __GASKET_PAGE_TABLE_H__ */