intel_gas.c revision 259512
1257251Skib/*-
2257251Skib * Copyright (c) 2013 The FreeBSD Foundation
3257251Skib * All rights reserved.
4257251Skib *
5257251Skib * This software was developed by Konstantin Belousov <kib@FreeBSD.org>
6257251Skib * under sponsorship from the FreeBSD Foundation.
7257251Skib *
8257251Skib * Redistribution and use in source and binary forms, with or without
9257251Skib * modification, are permitted provided that the following conditions
10257251Skib * are met:
11257251Skib * 1. Redistributions of source code must retain the above copyright
12257251Skib *    notice, this list of conditions and the following disclaimer.
13257251Skib * 2. Redistributions in binary form must reproduce the above copyright
14257251Skib *    notice, this list of conditions and the following disclaimer in the
15257251Skib *    documentation and/or other materials provided with the distribution.
16257251Skib *
17257251Skib * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18257251Skib * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19257251Skib * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20257251Skib * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21257251Skib * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22257251Skib * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23257251Skib * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24257251Skib * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25257251Skib * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26257251Skib * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27257251Skib * SUCH DAMAGE.
28257251Skib */
29257251Skib
30257251Skib#include <sys/cdefs.h>
31257251Skib__FBSDID("$FreeBSD: stable/10/sys/x86/iommu/intel_gas.c 259512 2013-12-17 13:49:35Z kib $");
32257251Skib
33257251Skib#define	RB_AUGMENT(entry) dmar_gas_augment_entry(entry)
34257251Skib
35257251Skib#include <sys/param.h>
36257251Skib#include <sys/systm.h>
37257251Skib#include <sys/malloc.h>
38257251Skib#include <sys/bus.h>
39257251Skib#include <sys/interrupt.h>
40257251Skib#include <sys/kernel.h>
41257251Skib#include <sys/ktr.h>
42257251Skib#include <sys/lock.h>
43257251Skib#include <sys/proc.h>
44257251Skib#include <sys/rwlock.h>
45257251Skib#include <sys/memdesc.h>
46257251Skib#include <sys/mutex.h>
47257251Skib#include <sys/sysctl.h>
48257251Skib#include <sys/rman.h>
49257251Skib#include <sys/taskqueue.h>
50257251Skib#include <sys/tree.h>
51257251Skib#include <sys/uio.h>
52257251Skib#include <dev/pci/pcivar.h>
53257251Skib#include <vm/vm.h>
54257251Skib#include <vm/vm_extern.h>
55257251Skib#include <vm/vm_kern.h>
56257251Skib#include <vm/vm_object.h>
57257251Skib#include <vm/vm_page.h>
58257251Skib#include <vm/vm_map.h>
59257251Skib#include <vm/uma.h>
60257251Skib#include <machine/atomic.h>
61257251Skib#include <machine/bus.h>
62257251Skib#include <machine/md_var.h>
63257251Skib#include <machine/specialreg.h>
64257251Skib#include <x86/include/busdma_impl.h>
65257251Skib#include <x86/iommu/intel_reg.h>
66257251Skib#include <x86/iommu/busdma_dmar.h>
67257251Skib#include <x86/iommu/intel_dmar.h>
68257251Skib
69257251Skib/*
70257251Skib * Guest Address Space management.
71257251Skib */
72257251Skib
73257251Skibstatic uma_zone_t dmar_map_entry_zone;
74257251Skib
75257251Skibstatic void
76257251Skibintel_gas_init(void)
77257251Skib{
78257251Skib
79257251Skib	dmar_map_entry_zone = uma_zcreate("DMAR_MAP_ENTRY",
80257251Skib	    sizeof(struct dmar_map_entry), NULL, NULL,
81257251Skib	    NULL, NULL, UMA_ALIGN_PTR, 0);
82257251Skib}
83257251SkibSYSINIT(intel_gas, SI_SUB_DRIVERS, SI_ORDER_FIRST, intel_gas_init, NULL);
84257251Skib
85257251Skibstruct dmar_map_entry *
86257251Skibdmar_gas_alloc_entry(struct dmar_ctx *ctx, u_int flags)
87257251Skib{
88257251Skib	struct dmar_map_entry *res;
89257251Skib
90257251Skib	KASSERT((flags & ~(DMAR_PGF_WAITOK)) == 0,
91257251Skib	    ("unsupported flags %x", flags));
92257251Skib
93257251Skib	res = uma_zalloc(dmar_map_entry_zone, ((flags & DMAR_PGF_WAITOK) !=
94257251Skib	    0 ? M_WAITOK : M_NOWAIT) | M_ZERO);
95259512Skib	if (res != NULL) {
96259512Skib		res->ctx = ctx;
97257251Skib		atomic_add_int(&ctx->entries_cnt, 1);
98259512Skib	}
99257251Skib	return (res);
100257251Skib}
101257251Skib
102257251Skibvoid
103257251Skibdmar_gas_free_entry(struct dmar_ctx *ctx, struct dmar_map_entry *entry)
104257251Skib{
105257251Skib
106259512Skib	KASSERT(ctx == entry->ctx,
107259512Skib	    ("mismatched free ctx %p entry %p entry->ctx %p", ctx,
108259512Skib	    entry, entry->ctx));
109257251Skib	atomic_subtract_int(&ctx->entries_cnt, 1);
110257251Skib	uma_zfree(dmar_map_entry_zone, entry);
111257251Skib}
112257251Skib
113257251Skibstatic int
114257251Skibdmar_gas_cmp_entries(struct dmar_map_entry *a, struct dmar_map_entry *b)
115257251Skib{
116257251Skib
117257251Skib	/* Last entry have zero size, so <= */
118257251Skib	KASSERT(a->start <= a->end, ("inverted entry %p (%jx, %jx)",
119257251Skib	    a, (uintmax_t)a->start, (uintmax_t)a->end));
120257251Skib	KASSERT(b->start <= b->end, ("inverted entry %p (%jx, %jx)",
121257251Skib	    b, (uintmax_t)b->start, (uintmax_t)b->end));
122257251Skib	KASSERT(a->end <= b->start || b->end <= a->start ||
123257251Skib	    a->end == a->start || b->end == b->start,
124257251Skib	    ("overlapping entries %p (%jx, %jx) %p (%jx, %jx)",
125257251Skib	    a, (uintmax_t)a->start, (uintmax_t)a->end,
126257251Skib	    b, (uintmax_t)b->start, (uintmax_t)b->end));
127257251Skib
128257251Skib	if (a->end < b->end)
129257251Skib		return (-1);
130257251Skib	else if (b->end < a->end)
131257251Skib		return (1);
132257251Skib	return (0);
133257251Skib}
134257251Skib
135257251Skibstatic void
136257251Skibdmar_gas_augment_entry(struct dmar_map_entry *entry)
137257251Skib{
138257251Skib	struct dmar_map_entry *l, *r;
139257251Skib
140257251Skib	for (; entry != NULL; entry = RB_PARENT(entry, rb_entry)) {
141257251Skib		l = RB_LEFT(entry, rb_entry);
142257251Skib		r = RB_RIGHT(entry, rb_entry);
143257251Skib		if (l == NULL && r == NULL) {
144257251Skib			entry->free_down = entry->free_after;
145257251Skib		} else if (l == NULL && r != NULL) {
146257251Skib			entry->free_down = MAX(entry->free_after, r->free_down);
147257251Skib		} else if (/*l != NULL && */ r == NULL) {
148257251Skib			entry->free_down = MAX(entry->free_after, l->free_down);
149257251Skib		} else /* if (l != NULL && r != NULL) */ {
150257251Skib			entry->free_down = MAX(entry->free_after, l->free_down);
151257251Skib			entry->free_down = MAX(entry->free_down, r->free_down);
152257251Skib		}
153257251Skib	}
154257251Skib}
155257251Skib
156257251SkibRB_GENERATE(dmar_gas_entries_tree, dmar_map_entry, rb_entry,
157257251Skib    dmar_gas_cmp_entries);
158257251Skib
159257251Skibstatic void
160257251Skibdmar_gas_fix_free(struct dmar_ctx *ctx, struct dmar_map_entry *entry)
161257251Skib{
162257251Skib	struct dmar_map_entry *next;
163257251Skib
164257251Skib	next = RB_NEXT(dmar_gas_entries_tree, &ctx->rb_root, entry);
165257251Skib	entry->free_after = (next != NULL ? next->start : ctx->end) -
166257251Skib	    entry->end;
167257251Skib	dmar_gas_augment_entry(entry);
168257251Skib}
169257251Skib
170257251Skib#ifdef INVARIANTS
171257251Skibstatic void
172257251Skibdmar_gas_check_free(struct dmar_ctx *ctx)
173257251Skib{
174257251Skib	struct dmar_map_entry *entry, *next, *l, *r;
175257251Skib	dmar_gaddr_t v;
176257251Skib
177257251Skib	RB_FOREACH(entry, dmar_gas_entries_tree, &ctx->rb_root) {
178259512Skib		KASSERT(ctx == entry->ctx,
179259512Skib		    ("mismatched free ctx %p entry %p entry->ctx %p", ctx,
180259512Skib		    entry, entry->ctx));
181257251Skib		next = RB_NEXT(dmar_gas_entries_tree, &ctx->rb_root, entry);
182257251Skib		if (next == NULL) {
183257251Skib			MPASS(entry->free_after == ctx->end - entry->end);
184257251Skib		} else {
185257251Skib			MPASS(entry->free_after = next->start - entry->end);
186257251Skib			MPASS(entry->end <= next->start);
187257251Skib		}
188257251Skib		l = RB_LEFT(entry, rb_entry);
189257251Skib		r = RB_RIGHT(entry, rb_entry);
190257251Skib		if (l == NULL && r == NULL) {
191257251Skib			MPASS(entry->free_down == entry->free_after);
192257251Skib		} else if (l == NULL && r != NULL) {
193257251Skib			MPASS(entry->free_down = MAX(entry->free_after,
194257251Skib			    r->free_down));
195257251Skib		} else if (r == NULL) {
196257251Skib			MPASS(entry->free_down = MAX(entry->free_after,
197257251Skib			    l->free_down));
198257251Skib		} else {
199257251Skib			v = MAX(entry->free_after, l->free_down);
200257251Skib			v = MAX(entry->free_down, r->free_down);
201257251Skib			MPASS(entry->free_down == v);
202257251Skib		}
203257251Skib	}
204257251Skib}
205257251Skib#endif
206257251Skib
207257251Skibstatic bool
208257251Skibdmar_gas_rb_insert(struct dmar_ctx *ctx, struct dmar_map_entry *entry)
209257251Skib{
210257251Skib	struct dmar_map_entry *prev, *found;
211257251Skib
212257251Skib	found = RB_INSERT(dmar_gas_entries_tree, &ctx->rb_root, entry);
213257251Skib	dmar_gas_fix_free(ctx, entry);
214257251Skib	prev = RB_PREV(dmar_gas_entries_tree, &ctx->rb_root, entry);
215257251Skib	if (prev != NULL)
216257251Skib		dmar_gas_fix_free(ctx, prev);
217257251Skib	return (found == NULL);
218257251Skib}
219257251Skib
220257251Skibstatic void
221257251Skibdmar_gas_rb_remove(struct dmar_ctx *ctx, struct dmar_map_entry *entry)
222257251Skib{
223257251Skib	struct dmar_map_entry *prev;
224257251Skib
225257251Skib	prev = RB_PREV(dmar_gas_entries_tree, &ctx->rb_root, entry);
226257251Skib	RB_REMOVE(dmar_gas_entries_tree, &ctx->rb_root, entry);
227257251Skib	if (prev != NULL)
228257251Skib		dmar_gas_fix_free(ctx, prev);
229257251Skib}
230257251Skib
231257251Skibvoid
232257251Skibdmar_gas_init_ctx(struct dmar_ctx *ctx)
233257251Skib{
234257251Skib	struct dmar_map_entry *begin, *end;
235257251Skib
236257251Skib	begin = dmar_gas_alloc_entry(ctx, DMAR_PGF_WAITOK);
237257251Skib	end = dmar_gas_alloc_entry(ctx, DMAR_PGF_WAITOK);
238257251Skib
239257251Skib	DMAR_CTX_LOCK(ctx);
240257251Skib	KASSERT(ctx->entries_cnt == 2, ("dirty ctx %p", ctx));
241257251Skib	KASSERT(RB_EMPTY(&ctx->rb_root), ("non-empty entries %p", ctx));
242257251Skib
243257251Skib	begin->start = 0;
244257251Skib	begin->end = DMAR_PAGE_SIZE;
245257251Skib	begin->free_after = ctx->end - begin->end;
246257251Skib	begin->flags = DMAR_MAP_ENTRY_PLACE | DMAR_MAP_ENTRY_UNMAPPED;
247257251Skib	dmar_gas_rb_insert(ctx, begin);
248257251Skib
249257251Skib	end->start = ctx->end;
250257251Skib	end->end = ctx->end;
251257251Skib	end->free_after = 0;
252257251Skib	end->flags = DMAR_MAP_ENTRY_PLACE | DMAR_MAP_ENTRY_UNMAPPED;
253257251Skib	dmar_gas_rb_insert(ctx, end);
254257251Skib
255257251Skib	ctx->first_place = begin;
256257251Skib	ctx->last_place = end;
257257251Skib	DMAR_CTX_UNLOCK(ctx);
258257251Skib}
259257251Skib
260257251Skibvoid
261257251Skibdmar_gas_fini_ctx(struct dmar_ctx *ctx)
262257251Skib{
263257251Skib	struct dmar_map_entry *entry, *entry1;
264257251Skib
265257251Skib	DMAR_CTX_ASSERT_LOCKED(ctx);
266257251Skib	KASSERT(ctx->entries_cnt == 2, ("ctx still in use %p", ctx));
267257251Skib
268257251Skib	entry = RB_MIN(dmar_gas_entries_tree, &ctx->rb_root);
269257251Skib	KASSERT(entry->start == 0, ("start entry start %p", ctx));
270257251Skib	KASSERT(entry->end == DMAR_PAGE_SIZE, ("start entry end %p", ctx));
271257251Skib	KASSERT(entry->flags == DMAR_MAP_ENTRY_PLACE,
272257251Skib	    ("start entry flags %p", ctx));
273257251Skib	RB_REMOVE(dmar_gas_entries_tree, &ctx->rb_root, entry);
274257251Skib	dmar_gas_free_entry(ctx, entry);
275257251Skib
276257251Skib	entry = RB_MAX(dmar_gas_entries_tree, &ctx->rb_root);
277257251Skib	KASSERT(entry->start == ctx->end, ("end entry start %p", ctx));
278257251Skib	KASSERT(entry->end == ctx->end, ("end entry end %p", ctx));
279257251Skib	KASSERT(entry->free_after == 0, ("end entry free_after%p", ctx));
280257251Skib	KASSERT(entry->flags == DMAR_MAP_ENTRY_PLACE,
281257251Skib	    ("end entry flags %p", ctx));
282257251Skib	RB_REMOVE(dmar_gas_entries_tree, &ctx->rb_root, entry);
283257251Skib	dmar_gas_free_entry(ctx, entry);
284257251Skib
285257251Skib	RB_FOREACH_SAFE(entry, dmar_gas_entries_tree, &ctx->rb_root, entry1) {
286257251Skib		KASSERT((entry->flags & DMAR_MAP_ENTRY_RMRR) != 0,
287257251Skib		    ("non-RMRR entry left %p", ctx));
288257251Skib		RB_REMOVE(dmar_gas_entries_tree, &ctx->rb_root, entry);
289257251Skib		dmar_gas_free_entry(ctx, entry);
290257251Skib	}
291257251Skib}
292257251Skib
293257251Skibstruct dmar_gas_match_args {
294257251Skib	struct dmar_ctx *ctx;
295257251Skib	dmar_gaddr_t size;
296257251Skib	const struct bus_dma_tag_common *common;
297257251Skib	u_int gas_flags;
298257251Skib	struct dmar_map_entry *entry;
299257251Skib};
300257251Skib
301257251Skibstatic bool
302257251Skibdmar_gas_match_one(struct dmar_gas_match_args *a, struct dmar_map_entry *prev,
303257251Skib    dmar_gaddr_t end)
304257251Skib{
305257251Skib	dmar_gaddr_t bs, start;
306257251Skib
307257251Skib	if (a->entry->start + a->size > end)
308257251Skib		return (false);
309257251Skib
310257251Skib	/* DMAR_PAGE_SIZE to create gap after new entry. */
311257251Skib	if (a->entry->start < prev->end + DMAR_PAGE_SIZE ||
312257251Skib	    a->entry->start + a->size + DMAR_PAGE_SIZE > prev->end +
313257251Skib	    prev->free_after)
314257251Skib		return (false);
315257251Skib
316257251Skib	/* No boundary crossing. */
317257251Skib	if (dmar_test_boundary(a->entry->start, a->size, a->common->boundary))
318257251Skib		return (true);
319257251Skib
320257251Skib	/*
321257251Skib	 * The start to start + size region crosses the boundary.
322257251Skib	 * Check if there is enough space after the next boundary
323257251Skib	 * after the prev->end.
324257251Skib	 */
325257251Skib	bs = (a->entry->start + a->common->boundary) & ~(a->common->boundary
326257251Skib	    - 1);
327257251Skib	start = roundup2(bs, a->common->alignment);
328257251Skib	/* DMAR_PAGE_SIZE to create gap after new entry. */
329257251Skib	if (start + a->size + DMAR_PAGE_SIZE <= prev->end + prev->free_after &&
330257251Skib	    start + a->size <= end) {
331257251Skib		a->entry->start = start;
332257251Skib		return (true);
333257251Skib	}
334257251Skib
335257251Skib	/*
336257251Skib	 * Not enough space to align at boundary, but allowed to split.
337257251Skib	 * We already checked that start + size does not overlap end.
338257251Skib	 *
339257251Skib	 * XXXKIB. It is possible that bs is exactly at the start of
340257251Skib	 * the next entry, then we do not have gap.  Ignore for now.
341257251Skib	 */
342257251Skib	if ((a->gas_flags & DMAR_GM_CANSPLIT) != 0) {
343257251Skib		a->size = bs - a->entry->start;
344257251Skib		return (true);
345257251Skib	}
346257251Skib
347257251Skib	return (false);
348257251Skib}
349257251Skib
350257251Skibstatic void
351257251Skibdmar_gas_match_insert(struct dmar_gas_match_args *a,
352257251Skib    struct dmar_map_entry *prev)
353257251Skib{
354257251Skib	struct dmar_map_entry *next;
355257251Skib	bool found;
356257251Skib
357257251Skib	/*
358257251Skib	 * The prev->end is always aligned on the page size, which
359257251Skib	 * causes page alignment for the entry->start too.  The size
360257251Skib	 * is checked to be multiple of the page size.
361257251Skib	 *
362257251Skib	 * The page sized gap is created between consequent
363257251Skib	 * allocations to ensure that out-of-bounds accesses fault.
364257251Skib	 */
365257251Skib	a->entry->end = a->entry->start + a->size;
366257251Skib
367257251Skib	next = RB_NEXT(dmar_gas_entries_tree, &a->ctx->rb_root, prev);
368257251Skib	KASSERT(next->start >= a->entry->end &&
369257251Skib	    next->start - a->entry->start >= a->size,
370257251Skib	    ("dmar_gas_match_insert hole failed %p prev (%jx, %jx) "
371257251Skib	    "free_after %jx next (%jx, %jx) entry (%jx, %jx)", a->ctx,
372257251Skib	    (uintmax_t)prev->start, (uintmax_t)prev->end,
373257251Skib	    (uintmax_t)prev->free_after,
374257251Skib	    (uintmax_t)next->start, (uintmax_t)next->end,
375257251Skib	    (uintmax_t)a->entry->start, (uintmax_t)a->entry->end));
376257251Skib
377257251Skib	prev->free_after = a->entry->start - prev->end;
378257251Skib	a->entry->free_after = next->start - a->entry->end;
379257251Skib
380257251Skib	found = dmar_gas_rb_insert(a->ctx, a->entry);
381257251Skib	KASSERT(found, ("found dup %p start %jx size %jx",
382257251Skib	    a->ctx, (uintmax_t)a->entry->start, (uintmax_t)a->size));
383257251Skib	a->entry->flags = DMAR_MAP_ENTRY_MAP;
384257251Skib
385257251Skib	KASSERT(RB_PREV(dmar_gas_entries_tree, &a->ctx->rb_root,
386257251Skib	    a->entry) == prev,
387257251Skib	    ("entry %p prev %p inserted prev %p", a->entry, prev,
388257251Skib	    RB_PREV(dmar_gas_entries_tree, &a->ctx->rb_root, a->entry)));
389257251Skib	KASSERT(RB_NEXT(dmar_gas_entries_tree, &a->ctx->rb_root,
390257251Skib	    a->entry) == next,
391257251Skib	    ("entry %p next %p inserted next %p", a->entry, next,
392257251Skib	    RB_NEXT(dmar_gas_entries_tree, &a->ctx->rb_root, a->entry)));
393257251Skib}
394257251Skib
395257251Skibstatic int
396257251Skibdmar_gas_lowermatch(struct dmar_gas_match_args *a, struct dmar_map_entry *prev)
397257251Skib{
398257251Skib	struct dmar_map_entry *l;
399257251Skib	int ret;
400257251Skib
401257251Skib	if (prev->end < a->common->lowaddr) {
402257251Skib		a->entry->start = roundup2(prev->end + DMAR_PAGE_SIZE,
403257251Skib		    a->common->alignment);
404257251Skib		if (dmar_gas_match_one(a, prev, a->common->lowaddr)) {
405257251Skib			dmar_gas_match_insert(a, prev);
406257251Skib			return (0);
407257251Skib		}
408257251Skib	}
409257251Skib	if (prev->free_down < a->size + DMAR_PAGE_SIZE)
410257251Skib		return (ENOMEM);
411257251Skib	l = RB_LEFT(prev, rb_entry);
412257251Skib	if (l != NULL) {
413257251Skib		ret = dmar_gas_lowermatch(a, l);
414257251Skib		if (ret == 0)
415257251Skib			return (0);
416257251Skib	}
417257251Skib	l = RB_RIGHT(prev, rb_entry);
418257251Skib	if (l != NULL)
419257251Skib		return (dmar_gas_lowermatch(a, l));
420257251Skib	return (ENOMEM);
421257251Skib}
422257251Skib
423257251Skibstatic int
424257251Skibdmar_gas_uppermatch(struct dmar_gas_match_args *a)
425257251Skib{
426257251Skib	struct dmar_map_entry *next, *prev, find_entry;
427257251Skib
428257251Skib	find_entry.start = a->common->highaddr;
429257251Skib	next = RB_NFIND(dmar_gas_entries_tree, &a->ctx->rb_root, &find_entry);
430257251Skib	if (next == NULL)
431257251Skib		return (ENOMEM);
432257251Skib	prev = RB_PREV(dmar_gas_entries_tree, &a->ctx->rb_root, next);
433257251Skib	KASSERT(prev != NULL, ("no prev %p %jx", a->ctx,
434257251Skib	    (uintmax_t)find_entry.start));
435257251Skib	for (;;) {
436257251Skib		a->entry->start = prev->start + DMAR_PAGE_SIZE;
437257251Skib		if (a->entry->start < a->common->highaddr)
438257251Skib			a->entry->start = a->common->highaddr;
439257251Skib		a->entry->start = roundup2(a->entry->start,
440257251Skib		    a->common->alignment);
441257251Skib		if (dmar_gas_match_one(a, prev, a->ctx->end)) {
442257251Skib			dmar_gas_match_insert(a, prev);
443257251Skib			return (0);
444257251Skib		}
445257251Skib
446257251Skib		/*
447257251Skib		 * XXXKIB.  This falls back to linear iteration over
448257251Skib		 * the free space in the high region.  But high
449257251Skib		 * regions are almost unused, the code should be
450257251Skib		 * enough to cover the case, although in the
451257251Skib		 * non-optimal way.
452257251Skib		 */
453257251Skib		prev = next;
454257251Skib		next = RB_NEXT(dmar_gas_entries_tree, &a->ctx->rb_root, prev);
455257251Skib		KASSERT(next != NULL, ("no next %p %jx", a->ctx,
456257251Skib		    (uintmax_t)find_entry.start));
457257251Skib		if (next->end >= a->ctx->end)
458257251Skib			return (ENOMEM);
459257251Skib	}
460257251Skib}
461257251Skib
462257251Skibstatic int
463257251Skibdmar_gas_find_space(struct dmar_ctx *ctx,
464257251Skib    const struct bus_dma_tag_common *common, dmar_gaddr_t size,
465257251Skib    u_int flags, struct dmar_map_entry *entry)
466257251Skib{
467257251Skib	struct dmar_gas_match_args a;
468257251Skib	int error;
469257251Skib
470257251Skib	DMAR_CTX_ASSERT_LOCKED(ctx);
471257251Skib	KASSERT(entry->flags == 0, ("dirty entry %p %p", ctx, entry));
472257251Skib	KASSERT((size & DMAR_PAGE_MASK) == 0, ("size %jx", (uintmax_t)size));
473257251Skib
474257251Skib	a.ctx = ctx;
475257251Skib	a.size = size;
476257251Skib	a.common = common;
477257251Skib	a.gas_flags = flags;
478257251Skib	a.entry = entry;
479257251Skib
480257251Skib	/* Handle lower region. */
481257251Skib	if (common->lowaddr > 0) {
482257251Skib		error = dmar_gas_lowermatch(&a, RB_ROOT(&ctx->rb_root));
483257251Skib		if (error == 0)
484257251Skib			return (0);
485257251Skib		KASSERT(error == ENOMEM,
486257251Skib		    ("error %d from dmar_gas_lowermatch", error));
487257251Skib	}
488257251Skib	/* Handle upper region. */
489257251Skib	if (common->highaddr >= ctx->end)
490257251Skib		return (ENOMEM);
491257251Skib	error = dmar_gas_uppermatch(&a);
492257251Skib	KASSERT(error == ENOMEM,
493257251Skib	    ("error %d from dmar_gas_uppermatch", error));
494257251Skib	return (error);
495257251Skib}
496257251Skib
497257251Skibstatic int
498257251Skibdmar_gas_alloc_region(struct dmar_ctx *ctx, struct dmar_map_entry *entry,
499257251Skib    u_int flags)
500257251Skib{
501257251Skib	struct dmar_map_entry *next, *prev;
502257251Skib	bool found;
503257251Skib
504257251Skib	DMAR_CTX_ASSERT_LOCKED(ctx);
505257251Skib
506257251Skib	if ((entry->start & DMAR_PAGE_MASK) != 0 ||
507257251Skib	    (entry->end & DMAR_PAGE_MASK) != 0)
508257251Skib		return (EINVAL);
509257251Skib	if (entry->start >= entry->end)
510257251Skib		return (EINVAL);
511257251Skib	if (entry->end >= ctx->end)
512257251Skib		return (EINVAL);
513257251Skib
514257251Skib	next = RB_NFIND(dmar_gas_entries_tree, &ctx->rb_root, entry);
515257251Skib	KASSERT(next != NULL, ("next must be non-null %p %jx", ctx,
516257251Skib	    (uintmax_t)entry->start));
517257251Skib	prev = RB_PREV(dmar_gas_entries_tree, &ctx->rb_root, next);
518257251Skib	/* prev could be NULL */
519257251Skib
520257251Skib	/*
521257251Skib	 * Adapt to broken BIOSes which specify overlapping RMRR
522257251Skib	 * entries.
523257251Skib	 *
524257251Skib	 * XXXKIB: this does not handle a case when prev or next
525257251Skib	 * entries are completely covered by the current one, which
526257251Skib	 * extends both ways.
527257251Skib	 */
528257251Skib	if (prev != NULL && prev->end > entry->start &&
529257251Skib	    (prev->flags & DMAR_MAP_ENTRY_PLACE) == 0) {
530257251Skib		if ((prev->flags & DMAR_MAP_ENTRY_RMRR) == 0)
531257251Skib			return (EBUSY);
532257251Skib		entry->start = prev->end;
533257251Skib	}
534257251Skib	if (next != NULL && next->start < entry->end &&
535257251Skib	    (next->flags & DMAR_MAP_ENTRY_PLACE) == 0) {
536257251Skib		if ((next->flags & DMAR_MAP_ENTRY_RMRR) == 0)
537257251Skib			return (EBUSY);
538257251Skib		entry->end = next->start;
539257251Skib	}
540257251Skib	if (entry->end == entry->start)
541257251Skib		return (0);
542257251Skib
543257251Skib	if (prev != NULL && prev->end > entry->start) {
544257251Skib		/* This assumes that prev is the placeholder entry. */
545257251Skib		dmar_gas_rb_remove(ctx, prev);
546257251Skib		prev = NULL;
547257251Skib	}
548257251Skib	if (next != NULL && next->start < entry->end) {
549257251Skib		dmar_gas_rb_remove(ctx, next);
550257251Skib		next = NULL;
551257251Skib	}
552257251Skib
553257251Skib	found = dmar_gas_rb_insert(ctx, entry);
554257251Skib	KASSERT(found, ("found RMRR dup %p start %jx end %jx",
555257251Skib	    ctx, (uintmax_t)entry->start, (uintmax_t)entry->end));
556257251Skib	entry->flags = DMAR_MAP_ENTRY_RMRR;
557257251Skib
558257251Skib#ifdef INVARIANTS
559257251Skib	struct dmar_map_entry *ip, *in;
560257251Skib	ip = RB_PREV(dmar_gas_entries_tree, &ctx->rb_root, entry);
561257251Skib	in = RB_NEXT(dmar_gas_entries_tree, &ctx->rb_root, entry);
562257251Skib	KASSERT(prev == NULL || ip == prev,
563257251Skib	    ("RMRR %p (%jx %jx) prev %p (%jx %jx) ins prev %p (%jx %jx)",
564257251Skib	    entry, entry->start, entry->end, prev,
565257251Skib	    prev == NULL ? 0 : prev->start, prev == NULL ? 0 : prev->end,
566257251Skib	    ip, ip == NULL ? 0 : ip->start, ip == NULL ? 0 : ip->end));
567257251Skib	KASSERT(next == NULL || in == next,
568257251Skib	    ("RMRR %p (%jx %jx) next %p (%jx %jx) ins next %p (%jx %jx)",
569257251Skib	    entry, entry->start, entry->end, next,
570257251Skib	    next == NULL ? 0 : next->start, next == NULL ? 0 : next->end,
571257251Skib	    in, in == NULL ? 0 : in->start, in == NULL ? 0 : in->end));
572257251Skib#endif
573257251Skib
574257251Skib	return (0);
575257251Skib}
576257251Skib
577257251Skibvoid
578257251Skibdmar_gas_free_space(struct dmar_ctx *ctx, struct dmar_map_entry *entry)
579257251Skib{
580257251Skib
581257251Skib	DMAR_CTX_ASSERT_LOCKED(ctx);
582257251Skib	KASSERT((entry->flags & (DMAR_MAP_ENTRY_PLACE | DMAR_MAP_ENTRY_RMRR |
583257251Skib	    DMAR_MAP_ENTRY_MAP)) == DMAR_MAP_ENTRY_MAP,
584257251Skib	    ("permanent entry %p %p", ctx, entry));
585257251Skib
586257251Skib	dmar_gas_rb_remove(ctx, entry);
587257251Skib	entry->flags &= ~DMAR_MAP_ENTRY_MAP;
588257251Skib#ifdef INVARIANTS
589257251Skib	if (dmar_check_free)
590257251Skib		dmar_gas_check_free(ctx);
591257251Skib#endif
592257251Skib}
593257251Skib
594259512Skibvoid
595257251Skibdmar_gas_free_region(struct dmar_ctx *ctx, struct dmar_map_entry *entry)
596257251Skib{
597257251Skib	struct dmar_map_entry *next, *prev;
598257251Skib
599257251Skib	DMAR_CTX_ASSERT_LOCKED(ctx);
600257251Skib	KASSERT((entry->flags & (DMAR_MAP_ENTRY_PLACE | DMAR_MAP_ENTRY_RMRR |
601257251Skib	    DMAR_MAP_ENTRY_MAP)) == DMAR_MAP_ENTRY_RMRR,
602257251Skib	    ("non-RMRR entry %p %p", ctx, entry));
603257251Skib
604257251Skib	prev = RB_PREV(dmar_gas_entries_tree, &ctx->rb_root, entry);
605257251Skib	next = RB_NEXT(dmar_gas_entries_tree, &ctx->rb_root, entry);
606257251Skib	dmar_gas_rb_remove(ctx, entry);
607257251Skib	entry->flags &= ~DMAR_MAP_ENTRY_RMRR;
608257251Skib
609257251Skib	if (prev == NULL)
610257251Skib		dmar_gas_rb_insert(ctx, ctx->first_place);
611257251Skib	if (next == NULL)
612257251Skib		dmar_gas_rb_insert(ctx, ctx->last_place);
613257251Skib}
614257251Skib
615257251Skibint
616257251Skibdmar_gas_map(struct dmar_ctx *ctx, const struct bus_dma_tag_common *common,
617257251Skib    dmar_gaddr_t size, u_int eflags, u_int flags, vm_page_t *ma,
618257251Skib    struct dmar_map_entry **res)
619257251Skib{
620257251Skib	struct dmar_map_entry *entry;
621257251Skib	int error;
622257251Skib
623257251Skib	KASSERT((flags & ~(DMAR_GM_CANWAIT | DMAR_GM_CANSPLIT)) == 0,
624257251Skib	    ("invalid flags 0x%x", flags));
625257251Skib
626257251Skib	entry = dmar_gas_alloc_entry(ctx, (flags & DMAR_GM_CANWAIT) != 0 ?
627257251Skib	    DMAR_PGF_WAITOK : 0);
628257251Skib	if (entry == NULL)
629257251Skib		return (ENOMEM);
630257251Skib	DMAR_CTX_LOCK(ctx);
631257251Skib	error = dmar_gas_find_space(ctx, common, size, flags, entry);
632257251Skib	if (error == ENOMEM) {
633257251Skib		DMAR_CTX_UNLOCK(ctx);
634257251Skib		dmar_gas_free_entry(ctx, entry);
635257251Skib		return (error);
636257251Skib	}
637257251Skib#ifdef INVARIANTS
638257251Skib	if (dmar_check_free)
639257251Skib		dmar_gas_check_free(ctx);
640257251Skib#endif
641257251Skib	KASSERT(error == 0,
642257251Skib	    ("unexpected error %d from dmar_gas_find_entry", error));
643257251Skib	KASSERT(entry->end < ctx->end, ("allocated GPA %jx, max GPA %jx",
644257251Skib	    (uintmax_t)entry->end, (uintmax_t)ctx->end));
645257251Skib	entry->flags |= eflags;
646257251Skib	DMAR_CTX_UNLOCK(ctx);
647257251Skib
648257251Skib	error = ctx_map_buf(ctx, entry->start, size, ma,
649257251Skib	    ((eflags & DMAR_MAP_ENTRY_READ) != 0 ? DMAR_PTE_R : 0) |
650257251Skib	    ((eflags & DMAR_MAP_ENTRY_WRITE) != 0 ? DMAR_PTE_W : 0) |
651257251Skib	    ((eflags & DMAR_MAP_ENTRY_SNOOP) != 0 ? DMAR_PTE_SNP : 0) |
652257251Skib	    ((eflags & DMAR_MAP_ENTRY_TM) != 0 ? DMAR_PTE_TM : 0),
653257251Skib	    (flags & DMAR_GM_CANWAIT) != 0 ? DMAR_PGF_WAITOK : 0);
654257251Skib	if (error == ENOMEM) {
655259512Skib		dmar_ctx_unload_entry(entry, true);
656257251Skib		return (error);
657257251Skib	}
658257251Skib	KASSERT(error == 0,
659257251Skib	    ("unexpected error %d from ctx_map_buf", error));
660257251Skib
661257251Skib	*res = entry;
662257251Skib	return (0);
663257251Skib}
664257251Skib
665257251Skibint
666257251Skibdmar_gas_map_region(struct dmar_ctx *ctx, struct dmar_map_entry *entry,
667257251Skib    u_int eflags, u_int flags, vm_page_t *ma)
668257251Skib{
669257251Skib	dmar_gaddr_t start;
670257251Skib	int error;
671257251Skib
672257251Skib	KASSERT(entry->flags == 0, ("used RMRR entry %p %p %x", ctx,
673257251Skib	    entry, entry->flags));
674257251Skib	KASSERT((flags & ~(DMAR_GM_CANWAIT)) == 0,
675257251Skib	    ("invalid flags 0x%x", flags));
676257251Skib
677257251Skib	start = entry->start;
678257251Skib	DMAR_CTX_LOCK(ctx);
679257251Skib	error = dmar_gas_alloc_region(ctx, entry, flags);
680257251Skib	if (error != 0) {
681257251Skib		DMAR_CTX_UNLOCK(ctx);
682257251Skib		return (error);
683257251Skib	}
684257251Skib	entry->flags |= eflags;
685257251Skib	DMAR_CTX_UNLOCK(ctx);
686257251Skib	if (entry->end == entry->start)
687257251Skib		return (0);
688257251Skib
689257251Skib	error = ctx_map_buf(ctx, entry->start, entry->end - entry->start,
690257251Skib	    ma + OFF_TO_IDX(start - entry->start),
691257251Skib	    ((eflags & DMAR_MAP_ENTRY_READ) != 0 ? DMAR_PTE_R : 0) |
692257251Skib	    ((eflags & DMAR_MAP_ENTRY_WRITE) != 0 ? DMAR_PTE_W : 0) |
693257251Skib	    ((eflags & DMAR_MAP_ENTRY_SNOOP) != 0 ? DMAR_PTE_SNP : 0) |
694257251Skib	    ((eflags & DMAR_MAP_ENTRY_TM) != 0 ? DMAR_PTE_TM : 0),
695257251Skib	    (flags & DMAR_GM_CANWAIT) != 0 ? DMAR_PGF_WAITOK : 0);
696257251Skib	if (error == ENOMEM) {
697259512Skib		dmar_ctx_unload_entry(entry, false);
698257251Skib		return (error);
699257251Skib	}
700257251Skib	KASSERT(error == 0,
701257251Skib	    ("unexpected error %d from ctx_map_buf", error));
702257251Skib
703257251Skib	return (0);
704257251Skib}
705257251Skib
706257251Skibint
707257251Skibdmar_gas_reserve_region(struct dmar_ctx *ctx, dmar_gaddr_t start,
708257251Skib    dmar_gaddr_t end)
709257251Skib{
710257251Skib	struct dmar_map_entry *entry;
711257251Skib	int error;
712257251Skib
713257251Skib	entry = dmar_gas_alloc_entry(ctx, DMAR_PGF_WAITOK);
714257251Skib	entry->start = start;
715257251Skib	entry->end = end;
716257251Skib	DMAR_CTX_LOCK(ctx);
717257251Skib	error = dmar_gas_alloc_region(ctx, entry, DMAR_GM_CANWAIT);
718257251Skib	if (error == 0)
719257251Skib		entry->flags |= DMAR_MAP_ENTRY_UNMAPPED;
720257251Skib	DMAR_CTX_UNLOCK(ctx);
721257251Skib	if (error != 0)
722257251Skib		dmar_gas_free_entry(ctx, entry);
723257251Skib	return (error);
724257251Skib}
725