--- /home/s/reference/FreeBSD/src/sys/amd64/amd64/efirt.c	2016-10-16 14:30:51.799107042 +0200
+++ sys/platform/pc64/x86_64/efirt.c	2016-11-22 20:57:45.570881000 +0100
@@ -27,25 +27,20 @@
  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
+ *
+ * $FreeBSD: head/sys/amd64/amd64/efirt.c 307391 2016-10-16 06:07:43Z kib $
  */
 
-#include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/amd64/amd64/efirt.c 307391 2016-10-16 06:07:43Z kib $");
-
 #include <sys/param.h>
 #include <sys/efi.h>
 #include <sys/kernel.h>
 #include <sys/linker.h>
 #include <sys/lock.h>
 #include <sys/module.h>
-#include <sys/mutex.h>
-#include <sys/clock.h>
 #include <sys/proc.h>
-#include <sys/rwlock.h>
 #include <sys/sched.h>
 #include <sys/sysctl.h>
 #include <sys/systm.h>
-#include <machine/fpu.h>
 #include <machine/efi.h>
 #include <machine/metadata.h>
 #include <machine/md_var.h>
@@ -99,7 +94,7 @@
 	return (code < nitems(efi_status2err) ? efi_status2err[code] : EDOOFUS);
 }
 
-static struct mtx efi_lock;
+static struct lock efi_lock;
 static pml4_entry_t *efi_pml4;
 static vm_object_t obj_1t1_pt;
 static vm_page_t efi_pml4_page;
@@ -192,9 +187,9 @@
 	uint64_t idx;
 	int bits, i, mode;
 
-	obj_1t1_pt = vm_pager_allocate(OBJT_PHYS, NULL, 1 + NPML4EPG +
+	obj_1t1_pt = phys_pager_alloc(NULL, 1 + NPML4EPG +
 	    NPML4EPG * NPDPEPG + NPML4EPG * NPDPEPG * NPDEPG,
-	    VM_PROT_ALL, 0, NULL);
+	    VM_PROT_ALL, 0);
 	VM_OBJECT_WLOCK(obj_1t1_pt);
 	efi_pml4_page = efi_1t1_page(0);
 	VM_OBJECT_WUNLOCK(obj_1t1_pt);
@@ -207,19 +202,19 @@
 			continue;
 		if (p->md_virt != NULL) {
 			if (bootverbose)
-				printf("EFI Runtime entry %d is mapped\n", i);
+				kprintf("EFI Runtime entry %d is mapped\n", i);
 			goto fail;
 		}
 		if ((p->md_phys & EFI_PAGE_MASK) != 0) {
 			if (bootverbose)
-				printf("EFI Runtime entry %d is not aligned\n",
+				kprintf("EFI Runtime entry %d is not aligned\n",
 				    i);
 			goto fail;
 		}
 		if (p->md_phys + p->md_pages * EFI_PAGE_SIZE < p->md_phys ||
 		    p->md_phys + p->md_pages * EFI_PAGE_SIZE >=
 		    VM_MAXUSER_ADDRESS) {
-			printf("EFI Runtime entry %d is not in mappable for RT:"
+			kprintf("EFI Runtime entry %d is not in mappable for RT:"
 			    "base %#016jx %#jx pages\n",
 			    i, (uintmax_t)p->md_phys,
 			    (uintmax_t)p->md_pages);
@@ -237,7 +232,7 @@
 			mode = VM_MEMATTR_UNCACHEABLE;
 		else {
 			if (bootverbose)
-				printf("EFI Runtime entry %d mapping "
+				kprintf("EFI Runtime entry %d mapping "
 				    "attributes unsupported\n", i);
 			mode = VM_MEMATTR_UNCACHEABLE;
 		}
@@ -296,13 +291,16 @@
 		return (ENXIO);
 	curpmap = PCPU_GET(curpmap);
 	PMAP_LOCK(curpmap);
-	mtx_lock(&efi_lock);
+	lockmgr(&efi_lock, LK_EXCLUSIVE);
 	error = fpu_kern_enter(curthread, NULL, FPU_KERN_NOCTX);
 	if (error != 0) {
 		PMAP_UNLOCK(curpmap);
 		return (error);
 	}
 
+#if __DragonFly__ /* XXX */
+	load_cr3(VM_PAGE_TO_PHYS(efi_pml4_page));
+#else
 	/*
 	 * IPI TLB shootdown handler invltlb_pcid_handler() reloads
 	 * %cr3 from the curpmap->pm_cr3, which would disable runtime
@@ -320,7 +318,8 @@
 	 * causes TLB invalidation.
 	 */
 	if (!pmap_pcid_enabled)
-		invltlb();
+#endif
+		cpu_invltlb();
 	return (0);
 }
 
@@ -330,15 +329,19 @@
 	pmap_t curpmap;
 
 	curpmap = &curproc->p_vmspace->vm_pmap;
+#if __DragonFly__ /* XXX */
+	load_cr3(curpmap->pm_cr3);
+#else
 	if (pmap_pcid_enabled && !invpcid_works)
 		PCPU_SET(curpmap, curpmap);
 	load_cr3(curpmap->pm_cr3 | (pmap_pcid_enabled ?
 	    curpmap->pm_pcids[PCPU_GET(cpuid)].pm_pcid : 0));
 	if (!pmap_pcid_enabled)
-		invltlb();
+#endif
+		cpu_invltlb();
 
 	fpu_kern_leave(curthread, NULL);
-	mtx_unlock(&efi_lock);
+	lockmgr(&efi_lock, LK_RELEASE);
 	PMAP_UNLOCK(curpmap);
 }
 
@@ -350,25 +353,25 @@
 	caddr_t kmdp;
 	size_t efisz;
 
-	mtx_init(&efi_lock, "efi", NULL, MTX_DEF);
+	lockinit(&efi_lock, "efi", 0, LK_CANRECURSE);
 
 	if (efi_systbl_phys == 0) {
 		if (bootverbose)
-			printf("EFI systbl not available\n");
+			kprintf("EFI systbl not available\n");
 		return (ENXIO);
 	}
 	efi_systbl = (struct efi_systbl *)PHYS_TO_DMAP(efi_systbl_phys);
 	if (efi_systbl->st_hdr.th_sig != EFI_SYSTBL_SIG) {
 		efi_systbl = NULL;
 		if (bootverbose)
-			printf("EFI systbl signature invalid\n");
+			kprintf("EFI systbl signature invalid\n");
 		return (ENXIO);
 	}
 	efi_cfgtbl = (efi_systbl->st_cfgtbl == 0) ? NULL :
 	    (struct efi_cfgtbl *)efi_systbl->st_cfgtbl;
 	if (efi_cfgtbl == NULL) {
 		if (bootverbose)
-			printf("EFI config table is not present\n");
+			kprintf("EFI config table is not present\n");
 	}
 
 	kmdp = preload_search_by_type("elf kernel");
@@ -378,7 +381,7 @@
 	    MODINFO_METADATA | MODINFOMD_EFI_MAP);
 	if (efihdr == NULL) {
 		if (bootverbose)
-			printf("EFI map is not present\n");
+			kprintf("EFI map is not present\n");
 		return (ENXIO);
 	}
 	efisz = (sizeof(struct efi_map_header) + 0xf) & ~0xf;
@@ -389,7 +392,7 @@
 	if (!efi_create_1t1_map(map, efihdr->memory_size /
 	    efihdr->descriptor_size, efihdr->descriptor_size)) {
 		if (bootverbose)
-			printf("EFI cannot create runtime map\n");
+			kprintf("EFI cannot create runtime map\n");
 		return (ENOMEM);
 	}
 
@@ -397,7 +400,7 @@
 	    (struct efi_rt *)efi_systbl->st_rt;
 	if (efi_runtime == NULL) {
 		if (bootverbose)
-			printf("EFI runtime services table is not present\n");
+			kprintf("EFI runtime services table is not present\n");
 		efi_destroy_1t1_map();
 		return (ENXIO);
 	}
@@ -415,7 +418,7 @@
 	efi_cfgtbl = NULL;
 	efi_runtime = NULL;
 
-	mtx_destroy(&efi_lock);
+	lockuninit(&efi_lock);
 }
 
 int
@@ -444,7 +447,7 @@
 	efi_status status;
 	int error;
 
-	mtx_assert(&resettodr_lock, MA_OWNED);
+	KKASSERT(lockowned(&resettodr_lock) != 0);
 	error = efi_enter();
 	if (error != 0)
 		return (error);
@@ -461,9 +464,9 @@
 
 	if (efi_runtime == NULL)
 		return (ENXIO);
-	mtx_lock(&resettodr_lock);
+	lockmgr(&resettodr_lock, LK_EXCLUSIVE);
 	error = efi_get_time_locked(tm);
-	mtx_unlock(&resettodr_lock);
+	lockmgr(&resettodr_lock, LK_RELEASE);
 	return (error);
 }
 
@@ -486,7 +489,7 @@
 	efi_status status;
 	int error;
 
-	mtx_assert(&resettodr_lock, MA_OWNED);
+	KKASSERT(lockowned(&resettodr_lock) != 0);
 	error = efi_enter();
 	if (error != 0)
 		return (error);
@@ -503,9 +506,9 @@
 
 	if (efi_runtime == NULL)
 		return (ENXIO);
-	mtx_lock(&resettodr_lock);
+	lockmgr(&resettodr_lock, LK_EXCLUSIVE);
 	error = efi_set_time_locked(tm);
-	mtx_unlock(&resettodr_lock);
+	lockmgr(&resettodr_lock, LK_RELEASE);
 	return (error);
 }
 
