]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - sys/mips/mips/mem.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / sys / mips / mips / mem.c
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 $  */
3 /*
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.
7  *
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.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
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.
23  *
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
34  * SUCH DAMAGE.
35  *
36  *      @(#)mem.c       8.3 (Berkeley) 1/12/94
37  *      JNPR: mem.c,v 1.3 2007/08/09 11:23:32 katta Exp $
38  */
39
40 /*
41  * Memory special file
42  */
43
44 #include <sys/cdefs.h>
45 __FBSDID("$FreeBSD$");
46
47 #include <sys/param.h>
48 #include <sys/kernel.h>
49 #include <sys/conf.h>
50 #include <sys/proc.h>
51 #include <sys/signalvar.h>
52 #include <vm/vm.h>
53 #include <vm/vm_extern.h>
54 #include <vm/pmap.h>
55 #include <vm/vm_map.h>
56 #include <sys/user.h>
57 #include <sys/msgbuf.h>
58 #include <sys/systm.h>
59 #include <sys/systm.h>
60 #include <sys/buf.h>
61 #include <sys/uio.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>
70
71
72 extern struct sysmaps sysmaps_pcpu[];
73 /*ARGSUSED*/
74 int
75 memrw(dev, uio, flags)
76         struct cdev *dev;
77         struct uio *uio;
78         int flags;
79 {
80         register vm_offset_t v;
81         register int c;
82         register struct iovec *iov;
83         int error = 0;
84
85         while (uio->uio_resid > 0 && error == 0) {
86                 iov = uio->uio_iov;
87                 if (iov->iov_len == 0) {
88                         uio->uio_iov++;
89                         uio->uio_iovcnt--;
90                         if (uio->uio_iovcnt < 0)
91                                 panic("mmrw");
92                         continue;
93                 }
94
95                 /* minor device 0 is physical memory */
96                 if (dev2unit(dev) == CDEV_MINOR_MEM) {
97                         v = uio->uio_offset;
98                         c = iov->iov_len;
99
100                         vm_offset_t va;
101                         vm_paddr_t pa;
102                         register int o;
103
104                         if (v + c > (SDRAM_ADDR_START + ctob(physmem)))
105                                 return (EFAULT);
106
107                         if (is_cacheable_mem(v) && is_cacheable_mem(v + c)) {
108                                 struct fpage *fp;
109                                 struct sysmaps *sysmaps;
110
111                                 sysmaps = &sysmaps_pcpu[PCPU_GET(cpuid)];
112                                 mtx_lock(&sysmaps->lock);
113                                 sched_pin();
114
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);
125                                 sched_unpin();
126                                 mtx_unlock(&sysmaps->lock);
127                         } else
128                                 return (EFAULT);
129                         continue;
130                 }
131
132                 /* minor device 1 is kernel memory */
133                 else if (dev2unit(dev) == CDEV_MINOR_KMEM) {
134                         v = uio->uio_offset;
135                         c = min(iov->iov_len, MAXPHYS);
136                         vm_offset_t addr, eaddr;
137                         vm_offset_t wired_tlb_virtmem_end;
138
139                         /*
140                          * Make sure that all of the pages are currently
141                          * resident so that we don't create any zero-fill pages.
142                          */
143                         addr = trunc_page(uio->uio_offset);
144                         eaddr = round_page(uio->uio_offset + c);
145
146                         if (addr < (vm_offset_t) VM_MIN_KERNEL_ADDRESS)
147                                 return EFAULT;
148
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;
154
155                         if (addr >= wired_tlb_virtmem_end) {
156                                 for (; addr < eaddr; addr += PAGE_SIZE) 
157                                         if (pmap_extract(kernel_pmap,addr) == 0)
158                                                 return EFAULT;
159
160                                 if (!kernacc((caddr_t)(int)uio->uio_offset, c,
161                                     uio->uio_rw == UIO_READ ?
162                                     VM_PROT_READ : VM_PROT_WRITE))
163                                         return (EFAULT);
164                         }
165
166                         error = uiomove((caddr_t)v, c, uio);
167                         continue;
168                 }
169
170         }
171         return (error);
172 }
173
174 /*ARGSUSED*/
175 int
176 memmmap(struct cdev *dev, vm_offset_t off, vm_paddr_t *paddr, int prot)
177 {
178
179         return (EOPNOTSUPP);
180 }
181
182 void
183 dev_mem_md_init(void)
184 {
185 }