1 /* $OpenBSD: mem.c,v 1.2 1998/08/31 17:42:34 millert Exp $ */
2 /* $NetBSD: mem.c,v 1.6 1995/04/10 11:55:03 mycroft Exp $ */
4 * Copyright (c) 1988 University of Utah.
5 * Copyright (c) 1982, 1986, 1990, 1993
6 * The Regents of the University of California. All rights reserved.
8 * This code is derived from software contributed to Berkeley by
9 * the Systems Programming Group of the University of Utah Computer
10 * Science Department and Ralph Campbell.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * @(#)mem.c 8.3 (Berkeley) 1/12/94
37 * JNPR: mem.c,v 1.3 2007/08/09 11:23:32 katta Exp $
44 #include <sys/cdefs.h>
45 __FBSDID("$FreeBSD$");
47 #include <sys/param.h>
48 #include <sys/kernel.h>
51 #include <sys/signalvar.h>
53 #include <vm/vm_extern.h>
55 #include <vm/vm_map.h>
57 #include <sys/msgbuf.h>
58 #include <sys/systm.h>
59 #include <sys/systm.h>
62 #include <sys/sched.h>
63 #include <sys/malloc.h>
64 #include <machine/pte.h>
65 #include <machine/cpu.h>
66 #include <machine/md_var.h>
67 #include <machine/atomic.h>
68 #include <machine/pltfm.h>
69 #include <machine/memdev.h>
72 extern struct sysmaps sysmaps_pcpu[];
75 memrw(dev, uio, flags)
80 register vm_offset_t v;
82 register struct iovec *iov;
85 while (uio->uio_resid > 0 && error == 0) {
87 if (iov->iov_len == 0) {
90 if (uio->uio_iovcnt < 0)
95 /* minor device 0 is physical memory */
96 if (dev2unit(dev) == CDEV_MINOR_MEM) {
104 if (v + c > (SDRAM_ADDR_START + ctob(physmem)))
107 if (is_cacheable_mem(v) && is_cacheable_mem(v + c)) {
109 struct sysmaps *sysmaps;
111 sysmaps = &sysmaps_pcpu[PCPU_GET(cpuid)];
112 mtx_lock(&sysmaps->lock);
115 fp = &sysmaps->fp[PMAP_FPAGE1];
116 pa = uio->uio_offset & ~PAGE_MASK;
117 va = pmap_map_fpage(pa, fp, FALSE);
118 o = (int)uio->uio_offset & PAGE_MASK;
119 c = (u_int)(PAGE_SIZE -
120 ((int)iov->iov_base & PAGE_MASK));
121 c = min(c, (u_int)(PAGE_SIZE - o));
122 c = min(c, (u_int)iov->iov_len);
123 error = uiomove((caddr_t)(va + o), (int)c, uio);
124 pmap_unmap_fpage(pa, fp);
126 mtx_unlock(&sysmaps->lock);
132 /* minor device 1 is kernel memory */
133 else if (dev2unit(dev) == CDEV_MINOR_KMEM) {
135 c = min(iov->iov_len, MAXPHYS);
136 vm_offset_t addr, eaddr;
137 vm_offset_t wired_tlb_virtmem_end;
140 * Make sure that all of the pages are currently
141 * resident so that we don't create any zero-fill pages.
143 addr = trunc_page(uio->uio_offset);
144 eaddr = round_page(uio->uio_offset + c);
146 if (addr < (vm_offset_t) VM_MIN_KERNEL_ADDRESS)
149 wired_tlb_virtmem_end = VM_MIN_KERNEL_ADDRESS +
150 VM_KERNEL_ALLOC_OFFSET;
151 if ((addr < wired_tlb_virtmem_end) &&
152 (eaddr >= wired_tlb_virtmem_end))
153 addr = wired_tlb_virtmem_end;
155 if (addr >= wired_tlb_virtmem_end) {
156 for (; addr < eaddr; addr += PAGE_SIZE)
157 if (pmap_extract(kernel_pmap,addr) == 0)
160 if (!kernacc((caddr_t)(int)uio->uio_offset, c,
161 uio->uio_rw == UIO_READ ?
162 VM_PROT_READ : VM_PROT_WRITE))
166 error = uiomove((caddr_t)v, c, uio);
176 memmmap(struct cdev *dev, vm_offset_t off, vm_paddr_t *paddr, int prot)
183 dev_mem_md_init(void)