]> git.saurik.com Git - apple/xnu.git/blob - osfmk/ppc/io_map.c
xnu-517.3.15.tar.gz
[apple/xnu.git] / osfmk / ppc / io_map.c
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * file.
14 *
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25 /*
26 * @OSF_COPYRIGHT@
27 *
28 */
29
30 #include <debug.h>
31 #include <mach/vm_param.h>
32 #include <vm/vm_kern.h>
33 #include <vm/vm_map.h>
34 #include <vm/vm_page.h>
35 #include <ppc/pmap.h>
36 #include <ppc/io_map_entries.h>
37 #include <ppc/Firmware.h>
38 #include <ppc/mappings.h>
39 #include <ppc/proc_reg.h>
40
41 extern vm_offset_t virtual_avail;
42
43 /*
44 * Allocate and map memory for devices that may need to be mapped
45 * outside the usual physical memory. If phys_addr is NULL then
46 * steal the appropriate number of physical pages from the vm
47 * system and map them.
48 *
49 * Note, this will onl
50 */
51 vm_offset_t
52 io_map(phys_addr, size)
53 vm_offset_t phys_addr;
54 vm_size_t size;
55 {
56 vm_offset_t start;
57 int i;
58 unsigned int j;
59 vm_page_t m;
60
61
62 #if DEBUG
63 assert (kernel_map != VM_MAP_NULL); /* VM must be initialised */
64 #endif
65
66 if (phys_addr != 0) { /* If they supplied a physical address, use it */
67
68 size = round_page_32(size + (phys_addr & PAGE_MASK)); /* Make sure we map all of it */
69
70 (void) kmem_alloc_pageable(kernel_map, &start, size); /* Get some virtual addresses to use */
71
72 (void)mapping_make(kernel_pmap, (addr64_t)start, (ppnum_t)(phys_addr >> 12),
73 (mmFlgBlock | mmFlgUseAttr | mmFlgCInhib | mmFlgGuarded), /* Map as I/O page */
74 size >> 12, VM_PROT_READ|VM_PROT_WRITE);
75
76 return (start + (phys_addr & PAGE_MASK)); /* Pass back the physical address */
77
78 } else {
79
80 (void) kmem_alloc_pageable(kernel_map, &start, size); /* Get some virtual addresses */
81
82 mapping_prealloc(size); /* Make sure there are enough free mappings */
83
84 for (i = 0; i < size ; i += PAGE_SIZE) {
85 m = VM_PAGE_NULL;
86 while ((m = vm_page_grab()) == VM_PAGE_NULL) { /* Get a physical page */
87 VM_PAGE_WAIT(); /* Wait if we didn't have one */
88 }
89 vm_page_gobble(m);
90
91 (void)mapping_make(kernel_pmap,
92 (addr64_t)(start + i), m->phys_page,
93 (mmFlgBlock | mmFlgUseAttr | mmFlgCInhib | mmFlgGuarded), /* Map as I/O page */
94 1, VM_PROT_READ|VM_PROT_WRITE);
95
96 }
97
98 mapping_relpre(); /* Allow mapping release */
99 return start;
100 }
101 }
102
103
104 /*
105 * Allocate and map memory for devices before the VM system comes alive.
106 */
107
108 vm_offset_t io_map_spec(vm_offset_t phys_addr, vm_size_t size)
109 {
110 vm_offset_t start;
111 int i;
112 unsigned int j;
113 vm_page_t m;
114
115
116 if(kernel_map != VM_MAP_NULL) { /* If VM system is up, redirect to normal routine */
117
118 return io_map(phys_addr, size); /* Map the address */
119
120 }
121
122 size = round_page_32(size + (phys_addr - (phys_addr & -PAGE_SIZE))); /* Extend the length to include it all */
123 start = pmap_boot_map(size); /* Get me some virtual address */
124
125 (void)mapping_make(kernel_pmap, (addr64_t)start, (ppnum_t)(phys_addr >> 12),
126 (mmFlgBlock | mmFlgUseAttr | mmFlgCInhib | mmFlgGuarded), /* Map as I/O page */
127 size >> 12, VM_PROT_READ|VM_PROT_WRITE);
128
129 return (start + (phys_addr & PAGE_MASK));
130 }