]> git.saurik.com Git - apple/xnu.git/blob - osfmk/ppc/io_map.c
xnu-344.49.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 vm_offset_t
50 io_map(phys_addr, size)
51 vm_offset_t phys_addr;
52 vm_size_t size;
53 {
54 vm_offset_t start;
55 int i;
56 unsigned int j;
57 vm_page_t m;
58
59
60 #if DEBUG
61 assert (kernel_map != VM_MAP_NULL); /* VM must be initialised */
62 #endif
63
64 if (phys_addr != 0) {
65 /* make sure we map full contents of all the pages concerned */
66 size = round_page(size + (phys_addr & PAGE_MASK));
67
68 /* Steal some free virtual addresses */
69
70 (void) kmem_alloc_pageable(kernel_map, &start, size);
71
72 pmap_map_block(kernel_pmap, start, phys_addr, size,
73 VM_PROT_READ|VM_PROT_WRITE, PTE_WIMG_IO, 0); /* Set up a block mapped area */
74
75 return (start + (phys_addr & PAGE_MASK));
76
77 } else {
78
79 /* Steal some free virtual addresses */
80 (void) kmem_alloc_pageable(kernel_map, &start, size);
81
82 mapping_prealloc(size); /* Make sure there are enough free mappings */
83 /* Steal some physical pages and map them one by one */
84 for (i = 0; i < size ; i += PAGE_SIZE) {
85 m = VM_PAGE_NULL;
86 while ((m = vm_page_grab()) == VM_PAGE_NULL)
87 VM_PAGE_WAIT();
88 vm_page_gobble(m);
89 (void) pmap_map_bd(start + i,
90 m->phys_addr,
91 m->phys_addr + PAGE_SIZE,
92 VM_PROT_READ|VM_PROT_WRITE);
93 }
94
95 mapping_relpre(); /* Allow mapping release */
96 return start;
97 }
98 }