Home | History | Annotate | Line # | Download | only in linux
linux_writecomb.c revision 1.2
      1 /*	$NetBSD: linux_writecomb.c,v 1.2 2014/11/04 11:27:31 jmcneill Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2013 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Taylor R. Campbell.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 __KERNEL_RCSID(0, "$NetBSD: linux_writecomb.c,v 1.2 2014/11/04 11:27:31 jmcneill Exp $");
     34 
     35 #ifdef _KERNEL_OPT
     36 #include "opt_mtrr.h"
     37 #endif
     38 
     39 #include <sys/kmem.h>
     40 #include <sys/mutex.h>
     41 
     42 #include <machine/mtrr.h>
     43 
     44 #include <linux/idr.h>
     45 #include <linux/io.h>
     46 
     47 static struct {
     48 	kmutex_t	lock;
     49 	struct idr	idr;
     50 } linux_writecomb __cacheline_aligned;
     51 
     52 int
     53 linux_writecomb_init(void)
     54 {
     55 
     56 	mutex_init(&linux_writecomb.lock, MUTEX_DEFAULT, IPL_SCHED);
     57 	idr_init(&linux_writecomb.idr);
     58 
     59 	return 0;
     60 }
     61 
     62 void
     63 linux_writecomb_fini(void)
     64 {
     65 
     66 	KASSERT(idr_is_empty(&linux_writecomb.idr));
     67 	idr_destroy(&linux_writecomb.idr);
     68 	mutex_destroy(&linux_writecomb.lock);
     69 }
     70 
     71 int
     72 arch_phys_wc_add(unsigned long base, unsigned long size)
     73 {
     74 	struct mtrr *mtrr;
     75 	int n = 1;
     76 	int id;
     77 	int ret;
     78 
     79 	mtrr = kmem_alloc(sizeof(*mtrr), KM_SLEEP);
     80 	mtrr->base = base;
     81 	mtrr->len = size;
     82 	mtrr->type = MTRR_TYPE_WC;
     83 	mtrr->flags = MTRR_VALID;
     84 
     85 	/* XXX errno NetBSD->Linux */
     86 	ret = -mtrr_set(mtrr, &n, NULL, MTRR_GETSET_KERNEL);
     87 	if (ret) {
     88 		KASSERT(n == 0);
     89 		goto fail0;
     90 	}
     91 	KASSERT(n == 1);
     92 
     93 	idr_preload(GFP_KERNEL);
     94 	mutex_spin_enter(&linux_writecomb.lock);
     95 	id = idr_alloc(&linux_writecomb.idr, mtrr, 0, 0, GFP_NOWAIT);
     96 	mutex_spin_exit(&linux_writecomb.lock);
     97 	idr_preload_end();
     98 	if (id < 0)
     99 		goto fail1;
    100 
    101 	return id;
    102 
    103 fail1:	KASSERT(id < 0);
    104 	mtrr->type = 0;
    105 	mtrr->flags = 0;
    106 	/* XXX errno NetBSD->Linux */
    107 	ret = -mtrr_set(mtrr, &n, NULL, MTRR_GETSET_KERNEL);
    108 	KASSERT(ret == 0);
    109 	KASSERT(n == 1);
    110 	ret = id;
    111 fail0:	KASSERT(ret < 0);
    112 	kmem_free(mtrr, sizeof(*mtrr));
    113 	return ret;
    114 }
    115 
    116 void
    117 arch_phys_wc_del(int id)
    118 {
    119 	struct mtrr *mtrr;
    120 	int n;
    121 	int ret __diagused;
    122 
    123 	KASSERT(0 <= id);
    124 
    125 	mutex_spin_enter(&linux_writecomb.lock);
    126 	mtrr = idr_find(&linux_writecomb.idr, id);
    127 	idr_remove(&linux_writecomb.idr, id);
    128 	mutex_spin_enter(&linux_writecomb.lock);
    129 
    130 	if (mtrr != NULL) {
    131 		mtrr->type = 0;
    132 		mtrr->flags = 0;
    133 		/* XXX errno NetBSD->Linux */
    134 		ret = -mtrr_set(mtrr, &n, NULL, MTRR_GETSET_KERNEL);
    135 		KASSERT(ret == 0);
    136 		KASSERT(n == 1);
    137 		kmem_free(mtrr, sizeof(*mtrr));
    138 	}
    139 }
    140 
    141 int
    142 phys_wc_to_mtrr_index(int handle)
    143 {
    144 
    145 	/* XXX Actually implement this...requires changes to our MTRR API.  */
    146 	return handle;
    147 }
    148