xref: /openbmc/linux/arch/ia64/kernel/uncached.c (revision 25763b3c864cf517d686661012d184ee47a49b4c)
1*25763b3cSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2f14f75b8SJes Sorensen /*
3e4a064dfSDean Nelson  * Copyright (C) 2001-2008 Silicon Graphics, Inc.  All rights reserved.
4f14f75b8SJes Sorensen  *
5f14f75b8SJes Sorensen  * A simple uncached page allocator using the generic allocator. This
6f14f75b8SJes Sorensen  * allocator first utilizes the spare (spill) pages found in the EFI
7f14f75b8SJes Sorensen  * memmap and will then start converting cached pages to uncached ones
8f14f75b8SJes Sorensen  * at a granule at a time. Node awareness is implemented by having a
9f14f75b8SJes Sorensen  * pool of pages per node.
10f14f75b8SJes Sorensen  */
11f14f75b8SJes Sorensen 
12f14f75b8SJes Sorensen #include <linux/types.h>
13f14f75b8SJes Sorensen #include <linux/kernel.h>
14f14f75b8SJes Sorensen #include <linux/module.h>
15f14f75b8SJes Sorensen #include <linux/init.h>
16f14f75b8SJes Sorensen #include <linux/errno.h>
17f14f75b8SJes Sorensen #include <linux/string.h>
18f14f75b8SJes Sorensen #include <linux/efi.h>
1938b8d208SIngo Molnar #include <linux/nmi.h>
20f14f75b8SJes Sorensen #include <linux/genalloc.h>
215a0e3ad6STejun Heo #include <linux/gfp.h>
22f14f75b8SJes Sorensen #include <asm/page.h>
23f14f75b8SJes Sorensen #include <asm/pal.h>
24f14f75b8SJes Sorensen #include <asm/pgtable.h>
2560063497SArun Sharma #include <linux/atomic.h>
26f14f75b8SJes Sorensen #include <asm/tlbflush.h>
27f14f75b8SJes Sorensen #include <asm/sn/arch.h>
28f14f75b8SJes Sorensen 
29f14f75b8SJes Sorensen 
30929f9727SDean Nelson extern void __init efi_memmap_walk_uc(efi_freemem_callback_t, void *);
31f14f75b8SJes Sorensen 
32eca7994fSDean Nelson struct uncached_pool {
33eca7994fSDean Nelson 	struct gen_pool *pool;
34eca7994fSDean Nelson 	struct mutex add_chunk_mutex;	/* serialize adding a converted chunk */
35eca7994fSDean Nelson 	int nchunks_added;		/* #of converted chunks added to pool */
36eca7994fSDean Nelson 	atomic_t status;		/* smp called function's return status*/
37eca7994fSDean Nelson };
38f14f75b8SJes Sorensen 
39eca7994fSDean Nelson #define MAX_CONVERTED_CHUNKS_PER_NODE	2
40eca7994fSDean Nelson 
41eca7994fSDean Nelson struct uncached_pool uncached_pools[MAX_NUMNODES];
42f14f75b8SJes Sorensen 
43f14f75b8SJes Sorensen 
44f14f75b8SJes Sorensen static void uncached_ipi_visibility(void *data)
45f14f75b8SJes Sorensen {
46f14f75b8SJes Sorensen 	int status;
47eca7994fSDean Nelson 	struct uncached_pool *uc_pool = (struct uncached_pool *)data;
48f14f75b8SJes Sorensen 
49f14f75b8SJes Sorensen 	status = ia64_pal_prefetch_visibility(PAL_VISIBILITY_PHYSICAL);
50f14f75b8SJes Sorensen 	if ((status != PAL_VISIBILITY_OK) &&
51f14f75b8SJes Sorensen 	    (status != PAL_VISIBILITY_OK_REMOTE_NEEDED))
52eca7994fSDean Nelson 		atomic_inc(&uc_pool->status);
53f14f75b8SJes Sorensen }
54f14f75b8SJes Sorensen 
55f14f75b8SJes Sorensen 
56f14f75b8SJes Sorensen static void uncached_ipi_mc_drain(void *data)
57f14f75b8SJes Sorensen {
58f14f75b8SJes Sorensen 	int status;
59eca7994fSDean Nelson 	struct uncached_pool *uc_pool = (struct uncached_pool *)data;
60929f9727SDean Nelson 
61f14f75b8SJes Sorensen 	status = ia64_pal_mc_drain();
62eca7994fSDean Nelson 	if (status != PAL_STATUS_SUCCESS)
63eca7994fSDean Nelson 		atomic_inc(&uc_pool->status);
64f14f75b8SJes Sorensen }
65f14f75b8SJes Sorensen 
66f14f75b8SJes Sorensen 
67929f9727SDean Nelson /*
68929f9727SDean Nelson  * Add a new chunk of uncached memory pages to the specified pool.
69929f9727SDean Nelson  *
70929f9727SDean Nelson  * @pool: pool to add new chunk of uncached memory to
71929f9727SDean Nelson  * @nid: node id of node to allocate memory from, or -1
72929f9727SDean Nelson  *
73929f9727SDean Nelson  * This is accomplished by first allocating a granule of cached memory pages
74929f9727SDean Nelson  * and then converting them to uncached memory pages.
75929f9727SDean Nelson  */
76eca7994fSDean Nelson static int uncached_add_chunk(struct uncached_pool *uc_pool, int nid)
77f14f75b8SJes Sorensen {
78f14f75b8SJes Sorensen 	struct page *page;
79eca7994fSDean Nelson 	int status, i, nchunks_added = uc_pool->nchunks_added;
80929f9727SDean Nelson 	unsigned long c_addr, uc_addr;
81f14f75b8SJes Sorensen 
82eca7994fSDean Nelson 	if (mutex_lock_interruptible(&uc_pool->add_chunk_mutex) != 0)
83eca7994fSDean Nelson 		return -1;	/* interrupted by a signal */
84eca7994fSDean Nelson 
85eca7994fSDean Nelson 	if (uc_pool->nchunks_added > nchunks_added) {
86eca7994fSDean Nelson 		/* someone added a new chunk while we were waiting */
87eca7994fSDean Nelson 		mutex_unlock(&uc_pool->add_chunk_mutex);
88eca7994fSDean Nelson 		return 0;
89eca7994fSDean Nelson 	}
90eca7994fSDean Nelson 
91eca7994fSDean Nelson 	if (uc_pool->nchunks_added >= MAX_CONVERTED_CHUNKS_PER_NODE) {
92eca7994fSDean Nelson 		mutex_unlock(&uc_pool->add_chunk_mutex);
93929f9727SDean Nelson 		return -1;
94eca7994fSDean Nelson 	}
95f14f75b8SJes Sorensen 
96929f9727SDean Nelson 	/* attempt to allocate a granule's worth of cached memory pages */
97929f9727SDean Nelson 
9896db800fSVlastimil Babka 	page = __alloc_pages_node(nid,
99e97ca8e5SJohannes Weiner 				GFP_KERNEL | __GFP_ZERO | __GFP_THISNODE,
100f14f75b8SJes Sorensen 				IA64_GRANULE_SHIFT-PAGE_SHIFT);
101eca7994fSDean Nelson 	if (!page) {
102eca7994fSDean Nelson 		mutex_unlock(&uc_pool->add_chunk_mutex);
103929f9727SDean Nelson 		return -1;
104eca7994fSDean Nelson 	}
105929f9727SDean Nelson 
106929f9727SDean Nelson 	/* convert the memory pages from cached to uncached */
107929f9727SDean Nelson 
108929f9727SDean Nelson 	c_addr = (unsigned long)page_address(page);
109929f9727SDean Nelson 	uc_addr = c_addr - PAGE_OFFSET + __IA64_UNCACHED_OFFSET;
110f14f75b8SJes Sorensen 
111f14f75b8SJes Sorensen 	/*
112f14f75b8SJes Sorensen 	 * There's a small race here where it's possible for someone to
113f14f75b8SJes Sorensen 	 * access the page through /dev/mem halfway through the conversion
114f14f75b8SJes Sorensen 	 * to uncached - not sure it's really worth bothering about
115f14f75b8SJes Sorensen 	 */
116f14f75b8SJes Sorensen 	for (i = 0; i < (IA64_GRANULE_SIZE / PAGE_SIZE); i++)
117f14f75b8SJes Sorensen 		SetPageUncached(&page[i]);
118f14f75b8SJes Sorensen 
119285fbd66SJan Beulich 	flush_tlb_kernel_range(uc_addr, uc_addr + IA64_GRANULE_SIZE);
120f14f75b8SJes Sorensen 
121f14f75b8SJes Sorensen 	status = ia64_pal_prefetch_visibility(PAL_VISIBILITY_PHYSICAL);
122eca7994fSDean Nelson 	if (status == PAL_VISIBILITY_OK_REMOTE_NEEDED) {
123eca7994fSDean Nelson 		atomic_set(&uc_pool->status, 0);
1248691e5a8SJens Axboe 		status = smp_call_function(uncached_ipi_visibility, uc_pool, 1);
125eca7994fSDean Nelson 		if (status || atomic_read(&uc_pool->status))
126929f9727SDean Nelson 			goto failed;
127eca7994fSDean Nelson 	} else if (status != PAL_VISIBILITY_OK)
128eca7994fSDean Nelson 		goto failed;
129f14f75b8SJes Sorensen 
130929f9727SDean Nelson 	preempt_disable();
131929f9727SDean Nelson 
132f14f75b8SJes Sorensen 	if (ia64_platform_is("sn2"))
133929f9727SDean Nelson 		sn_flush_all_caches(uc_addr, IA64_GRANULE_SIZE);
134f14f75b8SJes Sorensen 	else
135929f9727SDean Nelson 		flush_icache_range(uc_addr, uc_addr + IA64_GRANULE_SIZE);
136929f9727SDean Nelson 
137929f9727SDean Nelson 	/* flush the just introduced uncached translation from the TLB */
138929f9727SDean Nelson 	local_flush_tlb_all();
139929f9727SDean Nelson 
140929f9727SDean Nelson 	preempt_enable();
141f14f75b8SJes Sorensen 
142eca7994fSDean Nelson 	status = ia64_pal_mc_drain();
143eca7994fSDean Nelson 	if (status != PAL_STATUS_SUCCESS)
144eca7994fSDean Nelson 		goto failed;
145eca7994fSDean Nelson 	atomic_set(&uc_pool->status, 0);
1468691e5a8SJens Axboe 	status = smp_call_function(uncached_ipi_mc_drain, uc_pool, 1);
147eca7994fSDean Nelson 	if (status || atomic_read(&uc_pool->status))
148929f9727SDean Nelson 		goto failed;
149f14f75b8SJes Sorensen 
150929f9727SDean Nelson 	/*
151929f9727SDean Nelson 	 * The chunk of memory pages has been converted to uncached so now we
152929f9727SDean Nelson 	 * can add it to the pool.
153929f9727SDean Nelson 	 */
154eca7994fSDean Nelson 	status = gen_pool_add(uc_pool->pool, uc_addr, IA64_GRANULE_SIZE, nid);
155929f9727SDean Nelson 	if (status)
156929f9727SDean Nelson 		goto failed;
157f14f75b8SJes Sorensen 
158eca7994fSDean Nelson 	uc_pool->nchunks_added++;
159eca7994fSDean Nelson 	mutex_unlock(&uc_pool->add_chunk_mutex);
160929f9727SDean Nelson 	return 0;
161929f9727SDean Nelson 
162929f9727SDean Nelson 	/* failed to convert or add the chunk so give it back to the kernel */
163929f9727SDean Nelson failed:
164929f9727SDean Nelson 	for (i = 0; i < (IA64_GRANULE_SIZE / PAGE_SIZE); i++)
165929f9727SDean Nelson 		ClearPageUncached(&page[i]);
166929f9727SDean Nelson 
167929f9727SDean Nelson 	free_pages(c_addr, IA64_GRANULE_SHIFT-PAGE_SHIFT);
168eca7994fSDean Nelson 	mutex_unlock(&uc_pool->add_chunk_mutex);
169929f9727SDean Nelson 	return -1;
170f14f75b8SJes Sorensen }
171f14f75b8SJes Sorensen 
172f14f75b8SJes Sorensen 
173f14f75b8SJes Sorensen /*
174f14f75b8SJes Sorensen  * uncached_alloc_page
175f14f75b8SJes Sorensen  *
176929f9727SDean Nelson  * @starting_nid: node id of node to start with, or -1
177e4a064dfSDean Nelson  * @n_pages: number of contiguous pages to allocate
178929f9727SDean Nelson  *
179e4a064dfSDean Nelson  * Allocate the specified number of contiguous uncached pages on the
180e4a064dfSDean Nelson  * the requested node. If not enough contiguous uncached pages are available
181e4a064dfSDean Nelson  * on the requested node, roundrobin starting with the next higher node.
182f14f75b8SJes Sorensen  */
183e4a064dfSDean Nelson unsigned long uncached_alloc_page(int starting_nid, int n_pages)
184f14f75b8SJes Sorensen {
185929f9727SDean Nelson 	unsigned long uc_addr;
186eca7994fSDean Nelson 	struct uncached_pool *uc_pool;
187929f9727SDean Nelson 	int nid;
188f14f75b8SJes Sorensen 
189929f9727SDean Nelson 	if (unlikely(starting_nid >= MAX_NUMNODES))
190929f9727SDean Nelson 		return 0;
191f14f75b8SJes Sorensen 
192929f9727SDean Nelson 	if (starting_nid < 0)
193929f9727SDean Nelson 		starting_nid = numa_node_id();
194929f9727SDean Nelson 	nid = starting_nid;
195f14f75b8SJes Sorensen 
196929f9727SDean Nelson 	do {
1972dca53a9SChristoph Lameter 		if (!node_state(nid, N_HIGH_MEMORY))
198f14f75b8SJes Sorensen 			continue;
199eca7994fSDean Nelson 		uc_pool = &uncached_pools[nid];
200eca7994fSDean Nelson 		if (uc_pool->pool == NULL)
201929f9727SDean Nelson 			continue;
202929f9727SDean Nelson 		do {
203e4a064dfSDean Nelson 			uc_addr = gen_pool_alloc(uc_pool->pool,
204e4a064dfSDean Nelson 						 n_pages * PAGE_SIZE);
205929f9727SDean Nelson 			if (uc_addr != 0)
206929f9727SDean Nelson 				return uc_addr;
207eca7994fSDean Nelson 		} while (uncached_add_chunk(uc_pool, nid) == 0);
208f14f75b8SJes Sorensen 
209929f9727SDean Nelson 	} while ((nid = (nid + 1) % MAX_NUMNODES) != starting_nid);
210929f9727SDean Nelson 
211929f9727SDean Nelson 	return 0;
212f14f75b8SJes Sorensen }
213f14f75b8SJes Sorensen EXPORT_SYMBOL(uncached_alloc_page);
214f14f75b8SJes Sorensen 
215f14f75b8SJes Sorensen 
216f14f75b8SJes Sorensen /*
217f14f75b8SJes Sorensen  * uncached_free_page
218f14f75b8SJes Sorensen  *
219e4a064dfSDean Nelson  * @uc_addr: uncached address of first page to free
220e4a064dfSDean Nelson  * @n_pages: number of contiguous pages to free
221929f9727SDean Nelson  *
222e4a064dfSDean Nelson  * Free the specified number of uncached pages.
223f14f75b8SJes Sorensen  */
224e4a064dfSDean Nelson void uncached_free_page(unsigned long uc_addr, int n_pages)
225f14f75b8SJes Sorensen {
226929f9727SDean Nelson 	int nid = paddr_to_nid(uc_addr - __IA64_UNCACHED_OFFSET);
227eca7994fSDean Nelson 	struct gen_pool *pool = uncached_pools[nid].pool;
228f14f75b8SJes Sorensen 
229929f9727SDean Nelson 	if (unlikely(pool == NULL))
230929f9727SDean Nelson 		return;
231f14f75b8SJes Sorensen 
232929f9727SDean Nelson 	if ((uc_addr & (0XFUL << 60)) != __IA64_UNCACHED_OFFSET)
233929f9727SDean Nelson 		panic("uncached_free_page invalid address %lx\n", uc_addr);
234f14f75b8SJes Sorensen 
235e4a064dfSDean Nelson 	gen_pool_free(pool, uc_addr, n_pages * PAGE_SIZE);
236f14f75b8SJes Sorensen }
237f14f75b8SJes Sorensen EXPORT_SYMBOL(uncached_free_page);
238f14f75b8SJes Sorensen 
239f14f75b8SJes Sorensen 
240f14f75b8SJes Sorensen /*
241f14f75b8SJes Sorensen  * uncached_build_memmap,
242f14f75b8SJes Sorensen  *
243929f9727SDean Nelson  * @uc_start: uncached starting address of a chunk of uncached memory
244929f9727SDean Nelson  * @uc_end: uncached ending address of a chunk of uncached memory
245929f9727SDean Nelson  * @arg: ignored, (NULL argument passed in on call to efi_memmap_walk_uc())
246929f9727SDean Nelson  *
247f14f75b8SJes Sorensen  * Called at boot time to build a map of pages that can be used for
248f14f75b8SJes Sorensen  * memory special operations.
249f14f75b8SJes Sorensen  */
250e088a4adSMatthew Wilcox static int __init uncached_build_memmap(u64 uc_start, u64 uc_end, void *arg)
251f14f75b8SJes Sorensen {
252929f9727SDean Nelson 	int nid = paddr_to_nid(uc_start - __IA64_UNCACHED_OFFSET);
253eca7994fSDean Nelson 	struct gen_pool *pool = uncached_pools[nid].pool;
254929f9727SDean Nelson 	size_t size = uc_end - uc_start;
255f14f75b8SJes Sorensen 
256386d1d50SJohn Hawkes 	touch_softlockup_watchdog();
257f14f75b8SJes Sorensen 
258929f9727SDean Nelson 	if (pool != NULL) {
259929f9727SDean Nelson 		memset((char *)uc_start, 0, size);
260929f9727SDean Nelson 		(void) gen_pool_add(pool, uc_start, size, nid);
261f14f75b8SJes Sorensen 	}
262f14f75b8SJes Sorensen 	return 0;
263f14f75b8SJes Sorensen }
264f14f75b8SJes Sorensen 
265f14f75b8SJes Sorensen 
266929f9727SDean Nelson static int __init uncached_init(void)
267929f9727SDean Nelson {
268929f9727SDean Nelson 	int nid;
269f14f75b8SJes Sorensen 
2702dca53a9SChristoph Lameter 	for_each_node_state(nid, N_ONLINE) {
271eca7994fSDean Nelson 		uncached_pools[nid].pool = gen_pool_create(PAGE_SHIFT, nid);
272eca7994fSDean Nelson 		mutex_init(&uncached_pools[nid].add_chunk_mutex);
273f14f75b8SJes Sorensen 	}
274f14f75b8SJes Sorensen 
275929f9727SDean Nelson 	efi_memmap_walk_uc(uncached_build_memmap, NULL);
276f14f75b8SJes Sorensen 	return 0;
277f14f75b8SJes Sorensen }
278f14f75b8SJes Sorensen 
279f14f75b8SJes Sorensen __initcall(uncached_init);
280