2 * Copyright (c) 2000-2001 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 #include <sys/errno.h>
24 #include <kern/host.h>
25 #include <mach/mach_types.h>
26 #include <vm/vm_map.h>
27 #include <vm/vm_kern.h>
28 #include <vm/vm_pageout.h>
29 #include <mach/kern_return.h>
30 #include <mach/memory_object_types.h>
31 #include <mach/port.h>
32 #include <mach/policy.h>
33 #include <ipc/ipc_port.h>
34 #include <ipc/ipc_space.h>
35 #include <kern/thread.h>
36 #include <device/device_port.h>
37 #include <vm/vm_pageout.h>
39 /* Device VM COMPONENT INTERFACES */
47 /* until component support available */
51 /* until component support available */
52 int device_pager_workaround
;
54 typedef int device_port_t
;
56 typedef struct device_pager
{
57 int *pager
; /* pager workaround pointer */
58 unsigned int pager_ikot
; /* fake ip_kotype() */
59 unsigned int ref_count
; /* reference count */
60 memory_object_control_t control_handle
; /* mem object's cntrl handle */
61 device_port_t device_handle
; /* device_handle */
69 device_pager_bootstrap(
87 memory_object_control_t
,
92 device_pager_data_request(
94 memory_object_offset_t
,
99 device_pager_data_return(
101 memory_object_offset_t
,
107 device_pager_reference(
111 device_pager_deallocate(
115 device_pager_data_initialize(
117 memory_object_offset_t
,
121 device_pager_data_unlock(
123 memory_object_offset_t
,
128 device_pager_terminate(
132 device_pager_synchronize(
134 memory_object_offset_t
,
143 device_object_create(void);
145 zone_t device_pager_zone
;
148 #define DEVICE_PAGER_NULL ((device_pager_t) 0)
151 #define MAX_DNODE 10000
161 device_pager_bootstrap(void)
163 register vm_size_t size
;
165 size
= (vm_size_t
) sizeof(struct device_pager
);
166 device_pager_zone
= zinit(size
, (vm_size_t
) MAX_DNODE
*size
,
167 PAGE_SIZE
, "device node pager structures");
177 memory_object_t device
,
182 device_pager_t device_object
;
184 device_object
= device_object_create();
185 if (device_object
== DEVICE_PAGER_NULL
)
186 panic("device_pager_setup: device_object_create() failed");
188 device_object
->device_handle
= device_handle
;
189 device_object
->size
= size
;
190 device_object
->flags
= flags
;
192 return((memory_object_t
)device_object
);
199 device_pager_populate_object(
200 memory_object_t device
,
201 memory_object_offset_t offset
,
202 vm_offset_t phys_addr
,
205 device_pager_t device_object
;
206 vm_object_t vm_object
;
211 device_object
= device_pager_lookup(device
);
212 if(device_object
== DEVICE_PAGER_NULL
)
215 vm_object
= (vm_object_t
)memory_object_control_to_vm_object(
216 device_object
->control_handle
);
217 if(vm_object
== NULL
)
220 kr
= vm_object_populate_with_private(
221 vm_object
, offset
, phys_addr
, size
);
222 if(kr
!= KERN_SUCCESS
)
225 if(!vm_object
->phys_contiguous
) {
227 kr
= vm_object_upl_request(vm_object
,
228 (vm_object_offset_t
)offset
, size
, &upl
, NULL
,
229 &null_size
, (UPL_NO_SYNC
| UPL_CLEAN_IN_PLACE
));
231 if(kr
!= KERN_SUCCESS
)
232 panic("device_pager_populate_object: list_req failed");
234 upl_commit(upl
, NULL
);
247 memory_object_t name
)
249 device_pager_t device_object
;
251 device_object
= (device_pager_t
)name
;
252 assert(device_object
->pager
== &device_pager_workaround
);
253 return (device_object
);
260 device_pager_init(memory_object_t mem_obj
,
261 memory_object_control_t control
,
264 device_pager_t device_object
;
266 memory_object_attr_info_data_t attributes
;
268 vm_object_t vm_object
;
271 if (control
== MEMORY_OBJECT_CONTROL_NULL
)
272 return KERN_INVALID_ARGUMENT
;
274 device_object
= device_pager_lookup(mem_obj
);
276 memory_object_control_reference(control
);
277 device_object
->control_handle
= control
;
280 /* The following settings should be done through an expanded change */
281 /* attributes call */
283 vm_object
= (vm_object_t
)memory_object_control_to_vm_object(control
);
284 vm_object_lock(vm_object
);
285 vm_object
->private = TRUE
;
286 if(device_object
->flags
& DEVICE_PAGER_CONTIGUOUS
)
287 vm_object
->phys_contiguous
= TRUE
;
288 if(device_object
->flags
& DEVICE_PAGER_NOPHYSCACHE
)
289 vm_object
->nophyscache
= TRUE
;
291 vm_object
->wimg_bits
= device_object
->flags
& VM_WIMG_MASK
;
292 vm_object_unlock(vm_object
);
295 attributes
.copy_strategy
= MEMORY_OBJECT_COPY_DELAY
;
296 /* attributes.cluster_size = (1 << (CLUSTER_SHIFT + PAGE_SHIFT));*/
297 attributes
.cluster_size
= (1 << (PAGE_SHIFT
));
298 attributes
.may_cache_object
= FALSE
;
299 attributes
.temporary
= TRUE
;
301 kr
= memory_object_change_attributes(
303 MEMORY_OBJECT_ATTRIBUTE_INFO
,
304 (memory_object_info_t
) &attributes
,
305 MEMORY_OBJECT_ATTR_INFO_COUNT
);
306 if (kr
!= KERN_SUCCESS
)
307 panic("device_pager_init: memory_object_change_attributes() failed");
309 return(KERN_SUCCESS
);
316 device_pager_data_return(
317 memory_object_t mem_obj
,
318 memory_object_offset_t offset
,
321 boolean_t kernel_copy
)
323 device_pager_t device_object
;
325 device_object
= device_pager_lookup(mem_obj
);
326 if (device_object
== DEVICE_PAGER_NULL
)
327 panic("device_pager_data_return: lookup failed");
329 return device_data_action(device_object
->device_handle
, device_object
,
330 VM_PROT_READ
| VM_PROT_WRITE
, offset
, data_cnt
);
337 device_pager_data_request(
338 memory_object_t mem_obj
,
339 memory_object_offset_t offset
,
341 vm_prot_t protection_required
)
343 device_pager_t device_object
;
345 device_object
= device_pager_lookup(mem_obj
);
347 if (device_object
== DEVICE_PAGER_NULL
)
348 panic("device_pager_data_request: lookup failed");
350 device_data_action(device_object
->device_handle
, device_object
,
351 VM_PROT_READ
, offset
, length
);
359 device_pager_reference(
360 memory_object_t mem_obj
)
362 device_pager_t device_object
;
363 unsigned int new_ref_count
;
365 device_object
= device_pager_lookup(mem_obj
);
366 new_ref_count
= hw_atomic_add(&device_object
->ref_count
, 1);
367 assert(new_ref_count
> 1);
374 device_pager_deallocate(
375 memory_object_t mem_obj
)
377 device_pager_t device_object
;
379 device_object
= device_pager_lookup(mem_obj
);
381 if (hw_atomic_sub(&device_object
->ref_count
, 1) == 0) {
382 if (device_object
->device_handle
!= (device_port_t
) NULL
) {
383 device_close(device_object
->device_handle
);
385 zfree(device_pager_zone
, (vm_offset_t
) device_object
);
391 device_pager_data_initialize(
392 memory_object_t mem_obj
,
393 memory_object_offset_t offset
,
400 device_pager_data_unlock(
401 memory_object_t mem_obj
,
402 memory_object_offset_t offset
,
404 vm_prot_t desired_access
)
409 device_pager_terminate(
410 memory_object_t mem_obj
)
421 device_pager_synchronize(
422 memory_object_t mem_obj
,
423 memory_object_offset_t offset
,
425 vm_sync_t sync_flags
)
427 device_pager_t device_object
;
429 device_object
= device_pager_lookup(mem_obj
);
431 memory_object_synchronize_completed(
432 device_object
->control_handle
, offset
, length
);
442 memory_object_t mem_obj
)
453 device_object_create()
455 register device_pager_t device_object
;
457 device_object
= (struct device_pager
*) zalloc(device_pager_zone
);
458 if (device_object
== DEVICE_PAGER_NULL
)
459 return(DEVICE_PAGER_NULL
);
460 device_object
->pager
= &device_pager_workaround
;
461 device_object
->pager_ikot
= IKOT_MEMORY_OBJECT
;
462 device_object
->ref_count
= 1;
463 device_object
->control_handle
= MEMORY_OBJECT_CONTROL_NULL
;
466 return(device_object
);