180708Sjake/*-
280708Sjake * Copyright (c) 2001 Jake Burkholder.
380708Sjake * All rights reserved.
480708Sjake *
580708Sjake * Redistribution and use in source and binary forms, with or without
680708Sjake * modification, are permitted provided that the following conditions
780708Sjake * are met:
880708Sjake * 1. Redistributions of source code must retain the above copyright
980708Sjake *    notice, this list of conditions and the following disclaimer.
1080708Sjake * 2. Redistributions in binary form must reproduce the above copyright
1180708Sjake *    notice, this list of conditions and the following disclaimer in the
1280708Sjake *    documentation and/or other materials provided with the distribution.
1380708Sjake *
1481337Sobrien * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1580708Sjake * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1680708Sjake * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1781337Sobrien * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1880708Sjake * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1980708Sjake * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2080708Sjake * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2180708Sjake * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2280708Sjake * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2380708Sjake * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2480708Sjake * SUCH DAMAGE.
2580708Sjake */
2680708Sjake
27181701Smarius#include <sys/cdefs.h>
28181701Smarius__FBSDID("$FreeBSD$");
29181701Smarius
3080708Sjake#include <sys/param.h>
3180708Sjake#include <sys/systm.h>
32182730Smarius#include <sys/pcpu.h>
33182730Smarius#include <sys/proc.h>
34182730Smarius#include <sys/sched.h>
35182730Smarius
36182730Smarius#include <machine/cpu.h>
37182730Smarius#include <machine/cpufunc.h>
3880708Sjake
3980708Sjakevoid
40182730SmariusDELAY(int usec)
4180708Sjake{
42182730Smarius	u_long end;
43182730Smarius
44182730Smarius	if (usec < 0)
4584845Stmm		return;
46182730Smarius
47182730Smarius	/*
48182730Smarius	 * We avoid being migrated to another CPU with a possibly
49182730Smarius	 * unsynchronized TICK timer while spinning.
50182730Smarius	 */
51182730Smarius	sched_pin();
52182730Smarius
53182730Smarius	end = rd(tick) + (u_long)usec * PCPU_GET(clock) / 1000000;
54182730Smarius	while (rd(tick) < end)
55182730Smarius		cpu_spinwait();
56182730Smarius
57182730Smarius	sched_unpin();
58182730Smarius}
59182730Smarius
60