18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci *
38c2ecf20Sopenharmony_ci * page_pool.h
48c2ecf20Sopenharmony_ci *	Author:	Jesper Dangaard Brouer <netoptimizer@brouer.com>
58c2ecf20Sopenharmony_ci *	Copyright (C) 2016 Red Hat, Inc.
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci/**
98c2ecf20Sopenharmony_ci * DOC: page_pool allocator
108c2ecf20Sopenharmony_ci *
118c2ecf20Sopenharmony_ci * This page_pool allocator is optimized for the XDP mode that
128c2ecf20Sopenharmony_ci * uses one-frame-per-page, but have fallbacks that act like the
138c2ecf20Sopenharmony_ci * regular page allocator APIs.
148c2ecf20Sopenharmony_ci *
158c2ecf20Sopenharmony_ci * Basic use involve replacing alloc_pages() calls with the
168c2ecf20Sopenharmony_ci * page_pool_alloc_pages() call.  Drivers should likely use
178c2ecf20Sopenharmony_ci * page_pool_dev_alloc_pages() replacing dev_alloc_pages().
188c2ecf20Sopenharmony_ci *
198c2ecf20Sopenharmony_ci * API keeps track of in-flight pages, in-order to let API user know
208c2ecf20Sopenharmony_ci * when it is safe to dealloactor page_pool object.  Thus, API users
218c2ecf20Sopenharmony_ci * must make sure to call page_pool_release_page() when a page is
228c2ecf20Sopenharmony_ci * "leaving" the page_pool.  Or call page_pool_put_page() where
238c2ecf20Sopenharmony_ci * appropiate.  For maintaining correct accounting.
248c2ecf20Sopenharmony_ci *
258c2ecf20Sopenharmony_ci * API user must only call page_pool_put_page() once on a page, as it
268c2ecf20Sopenharmony_ci * will either recycle the page, or in case of elevated refcnt, it
278c2ecf20Sopenharmony_ci * will release the DMA mapping and in-flight state accounting.  We
288c2ecf20Sopenharmony_ci * hope to lift this requirement in the future.
298c2ecf20Sopenharmony_ci */
308c2ecf20Sopenharmony_ci#ifndef _NET_PAGE_POOL_H
318c2ecf20Sopenharmony_ci#define _NET_PAGE_POOL_H
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci#include <linux/mm.h> /* Needed by ptr_ring */
348c2ecf20Sopenharmony_ci#include <linux/ptr_ring.h>
358c2ecf20Sopenharmony_ci#include <linux/dma-direction.h>
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci#define PP_FLAG_DMA_MAP		BIT(0) /* Should page_pool do the DMA
388c2ecf20Sopenharmony_ci					* map/unmap
398c2ecf20Sopenharmony_ci					*/
408c2ecf20Sopenharmony_ci#define PP_FLAG_DMA_SYNC_DEV	BIT(1) /* If set all pages that the driver gets
418c2ecf20Sopenharmony_ci					* from page_pool will be
428c2ecf20Sopenharmony_ci					* DMA-synced-for-device according to
438c2ecf20Sopenharmony_ci					* the length provided by the device
448c2ecf20Sopenharmony_ci					* driver.
458c2ecf20Sopenharmony_ci					* Please note DMA-sync-for-CPU is still
468c2ecf20Sopenharmony_ci					* device driver responsibility
478c2ecf20Sopenharmony_ci					*/
488c2ecf20Sopenharmony_ci#define PP_FLAG_ALL		(PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV)
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ci/*
518c2ecf20Sopenharmony_ci * Fast allocation side cache array/stack
528c2ecf20Sopenharmony_ci *
538c2ecf20Sopenharmony_ci * The cache size and refill watermark is related to the network
548c2ecf20Sopenharmony_ci * use-case.  The NAPI budget is 64 packets.  After a NAPI poll the RX
558c2ecf20Sopenharmony_ci * ring is usually refilled and the max consumed elements will be 64,
568c2ecf20Sopenharmony_ci * thus a natural max size of objects needed in the cache.
578c2ecf20Sopenharmony_ci *
588c2ecf20Sopenharmony_ci * Keeping room for more objects, is due to XDP_DROP use-case.  As
598c2ecf20Sopenharmony_ci * XDP_DROP allows the opportunity to recycle objects directly into
608c2ecf20Sopenharmony_ci * this array, as it shares the same softirq/NAPI protection.  If
618c2ecf20Sopenharmony_ci * cache is already full (or partly full) then the XDP_DROP recycles
628c2ecf20Sopenharmony_ci * would have to take a slower code path.
638c2ecf20Sopenharmony_ci */
648c2ecf20Sopenharmony_ci#define PP_ALLOC_CACHE_SIZE	128
658c2ecf20Sopenharmony_ci#define PP_ALLOC_CACHE_REFILL	64
668c2ecf20Sopenharmony_cistruct pp_alloc_cache {
678c2ecf20Sopenharmony_ci	u32 count;
688c2ecf20Sopenharmony_ci	void *cache[PP_ALLOC_CACHE_SIZE];
698c2ecf20Sopenharmony_ci};
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_cistruct page_pool_params {
728c2ecf20Sopenharmony_ci	unsigned int	flags;
738c2ecf20Sopenharmony_ci	unsigned int	order;
748c2ecf20Sopenharmony_ci	unsigned int	pool_size;
758c2ecf20Sopenharmony_ci	int		nid;  /* Numa node id to allocate from pages from */
768c2ecf20Sopenharmony_ci	struct device	*dev; /* device, for DMA pre-mapping purposes */
778c2ecf20Sopenharmony_ci	enum dma_data_direction dma_dir; /* DMA mapping direction */
788c2ecf20Sopenharmony_ci	unsigned int	max_len; /* max DMA sync memory size */
798c2ecf20Sopenharmony_ci	unsigned int	offset;  /* DMA addr offset */
808c2ecf20Sopenharmony_ci};
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_cistruct page_pool {
838c2ecf20Sopenharmony_ci	struct page_pool_params p;
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci	struct delayed_work release_dw;
868c2ecf20Sopenharmony_ci	void (*disconnect)(void *);
878c2ecf20Sopenharmony_ci	unsigned long defer_start;
888c2ecf20Sopenharmony_ci	unsigned long defer_warn;
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ci	u32 pages_state_hold_cnt;
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci	/*
938c2ecf20Sopenharmony_ci	 * Data structure for allocation side
948c2ecf20Sopenharmony_ci	 *
958c2ecf20Sopenharmony_ci	 * Drivers allocation side usually already perform some kind
968c2ecf20Sopenharmony_ci	 * of resource protection.  Piggyback on this protection, and
978c2ecf20Sopenharmony_ci	 * require driver to protect allocation side.
988c2ecf20Sopenharmony_ci	 *
998c2ecf20Sopenharmony_ci	 * For NIC drivers this means, allocate a page_pool per
1008c2ecf20Sopenharmony_ci	 * RX-queue. As the RX-queue is already protected by
1018c2ecf20Sopenharmony_ci	 * Softirq/BH scheduling and napi_schedule. NAPI schedule
1028c2ecf20Sopenharmony_ci	 * guarantee that a single napi_struct will only be scheduled
1038c2ecf20Sopenharmony_ci	 * on a single CPU (see napi_schedule).
1048c2ecf20Sopenharmony_ci	 */
1058c2ecf20Sopenharmony_ci	struct pp_alloc_cache alloc ____cacheline_aligned_in_smp;
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ci	/* Data structure for storing recycled pages.
1088c2ecf20Sopenharmony_ci	 *
1098c2ecf20Sopenharmony_ci	 * Returning/freeing pages is more complicated synchronization
1108c2ecf20Sopenharmony_ci	 * wise, because free's can happen on remote CPUs, with no
1118c2ecf20Sopenharmony_ci	 * association with allocation resource.
1128c2ecf20Sopenharmony_ci	 *
1138c2ecf20Sopenharmony_ci	 * Use ptr_ring, as it separates consumer and producer
1148c2ecf20Sopenharmony_ci	 * effeciently, it a way that doesn't bounce cache-lines.
1158c2ecf20Sopenharmony_ci	 *
1168c2ecf20Sopenharmony_ci	 * TODO: Implement bulk return pages into this structure.
1178c2ecf20Sopenharmony_ci	 */
1188c2ecf20Sopenharmony_ci	struct ptr_ring ring;
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci	atomic_t pages_state_release_cnt;
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_ci	/* A page_pool is strictly tied to a single RX-queue being
1238c2ecf20Sopenharmony_ci	 * protected by NAPI, due to above pp_alloc_cache. This
1248c2ecf20Sopenharmony_ci	 * refcnt serves purpose is to simplify drivers error handling.
1258c2ecf20Sopenharmony_ci	 */
1268c2ecf20Sopenharmony_ci	refcount_t user_cnt;
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_ci	u64 destroy_cnt;
1298c2ecf20Sopenharmony_ci};
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_cistruct page *page_pool_alloc_pages(struct page_pool *pool, gfp_t gfp);
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_cistatic inline struct page *page_pool_dev_alloc_pages(struct page_pool *pool)
1348c2ecf20Sopenharmony_ci{
1358c2ecf20Sopenharmony_ci	gfp_t gfp = (GFP_ATOMIC | __GFP_NOWARN);
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_ci	return page_pool_alloc_pages(pool, gfp);
1388c2ecf20Sopenharmony_ci}
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_ci/* get the stored dma direction. A driver might decide to treat this locally and
1418c2ecf20Sopenharmony_ci * avoid the extra cache line from page_pool to determine the direction
1428c2ecf20Sopenharmony_ci */
1438c2ecf20Sopenharmony_cistatic
1448c2ecf20Sopenharmony_ciinline enum dma_data_direction page_pool_get_dma_dir(struct page_pool *pool)
1458c2ecf20Sopenharmony_ci{
1468c2ecf20Sopenharmony_ci	return pool->p.dma_dir;
1478c2ecf20Sopenharmony_ci}
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_cistruct page_pool *page_pool_create(const struct page_pool_params *params);
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_ci#ifdef CONFIG_PAGE_POOL
1528c2ecf20Sopenharmony_civoid page_pool_destroy(struct page_pool *pool);
1538c2ecf20Sopenharmony_civoid page_pool_use_xdp_mem(struct page_pool *pool, void (*disconnect)(void *));
1548c2ecf20Sopenharmony_civoid page_pool_release_page(struct page_pool *pool, struct page *page);
1558c2ecf20Sopenharmony_ci#else
1568c2ecf20Sopenharmony_cistatic inline void page_pool_destroy(struct page_pool *pool)
1578c2ecf20Sopenharmony_ci{
1588c2ecf20Sopenharmony_ci}
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_cistatic inline void page_pool_use_xdp_mem(struct page_pool *pool,
1618c2ecf20Sopenharmony_ci					 void (*disconnect)(void *))
1628c2ecf20Sopenharmony_ci{
1638c2ecf20Sopenharmony_ci}
1648c2ecf20Sopenharmony_cistatic inline void page_pool_release_page(struct page_pool *pool,
1658c2ecf20Sopenharmony_ci					  struct page *page)
1668c2ecf20Sopenharmony_ci{
1678c2ecf20Sopenharmony_ci}
1688c2ecf20Sopenharmony_ci#endif
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_civoid page_pool_put_page(struct page_pool *pool, struct page *page,
1718c2ecf20Sopenharmony_ci			unsigned int dma_sync_size, bool allow_direct);
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_ci/* Same as above but will try to sync the entire area pool->max_len */
1748c2ecf20Sopenharmony_cistatic inline void page_pool_put_full_page(struct page_pool *pool,
1758c2ecf20Sopenharmony_ci					   struct page *page, bool allow_direct)
1768c2ecf20Sopenharmony_ci{
1778c2ecf20Sopenharmony_ci	/* When page_pool isn't compiled-in, net/core/xdp.c doesn't
1788c2ecf20Sopenharmony_ci	 * allow registering MEM_TYPE_PAGE_POOL, but shield linker.
1798c2ecf20Sopenharmony_ci	 */
1808c2ecf20Sopenharmony_ci#ifdef CONFIG_PAGE_POOL
1818c2ecf20Sopenharmony_ci	page_pool_put_page(pool, page, -1, allow_direct);
1828c2ecf20Sopenharmony_ci#endif
1838c2ecf20Sopenharmony_ci}
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_ci/* Same as above but the caller must guarantee safe context. e.g NAPI */
1868c2ecf20Sopenharmony_cistatic inline void page_pool_recycle_direct(struct page_pool *pool,
1878c2ecf20Sopenharmony_ci					    struct page *page)
1888c2ecf20Sopenharmony_ci{
1898c2ecf20Sopenharmony_ci	page_pool_put_full_page(pool, page, true);
1908c2ecf20Sopenharmony_ci}
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_cistatic inline dma_addr_t page_pool_get_dma_addr(struct page *page)
1938c2ecf20Sopenharmony_ci{
1948c2ecf20Sopenharmony_ci	dma_addr_t ret = page->dma_addr[0];
1958c2ecf20Sopenharmony_ci	if (sizeof(dma_addr_t) > sizeof(unsigned long))
1968c2ecf20Sopenharmony_ci		ret |= (dma_addr_t)page->dma_addr[1] << 16 << 16;
1978c2ecf20Sopenharmony_ci	return ret;
1988c2ecf20Sopenharmony_ci}
1998c2ecf20Sopenharmony_ci
2008c2ecf20Sopenharmony_cistatic inline void page_pool_set_dma_addr(struct page *page, dma_addr_t addr)
2018c2ecf20Sopenharmony_ci{
2028c2ecf20Sopenharmony_ci	page->dma_addr[0] = addr;
2038c2ecf20Sopenharmony_ci	if (sizeof(dma_addr_t) > sizeof(unsigned long))
2048c2ecf20Sopenharmony_ci		page->dma_addr[1] = upper_32_bits(addr);
2058c2ecf20Sopenharmony_ci}
2068c2ecf20Sopenharmony_ci
2078c2ecf20Sopenharmony_cistatic inline bool is_page_pool_compiled_in(void)
2088c2ecf20Sopenharmony_ci{
2098c2ecf20Sopenharmony_ci#ifdef CONFIG_PAGE_POOL
2108c2ecf20Sopenharmony_ci	return true;
2118c2ecf20Sopenharmony_ci#else
2128c2ecf20Sopenharmony_ci	return false;
2138c2ecf20Sopenharmony_ci#endif
2148c2ecf20Sopenharmony_ci}
2158c2ecf20Sopenharmony_ci
2168c2ecf20Sopenharmony_cistatic inline bool page_pool_put(struct page_pool *pool)
2178c2ecf20Sopenharmony_ci{
2188c2ecf20Sopenharmony_ci	return refcount_dec_and_test(&pool->user_cnt);
2198c2ecf20Sopenharmony_ci}
2208c2ecf20Sopenharmony_ci
2218c2ecf20Sopenharmony_ci/* Caller must provide appropriate safe context, e.g. NAPI. */
2228c2ecf20Sopenharmony_civoid page_pool_update_nid(struct page_pool *pool, int new_nid);
2238c2ecf20Sopenharmony_cistatic inline void page_pool_nid_changed(struct page_pool *pool, int new_nid)
2248c2ecf20Sopenharmony_ci{
2258c2ecf20Sopenharmony_ci	if (unlikely(pool->p.nid != new_nid))
2268c2ecf20Sopenharmony_ci		page_pool_update_nid(pool, new_nid);
2278c2ecf20Sopenharmony_ci}
2288c2ecf20Sopenharmony_ci#endif /* _NET_PAGE_POOL_H */
229