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