2 * Copyright 2004-2009, Marcus Overhagen. All rights reserved.
3 * Distributed under the terms of the MIT License.
7 #include <KernelExport.h>
13 #define TRACE(a...) dprintf("ahci: " a)
14 #define ERROR(a...) dprintf("ahci: " a)
18 round_to_pagesize(uint32 size
)
20 return (size
+ B_PAGE_SIZE
- 1) & ~(B_PAGE_SIZE
- 1);
25 alloc_mem(void **virt
, phys_addr_t
*phy
, size_t size
, uint32 protection
,
33 TRACE("allocating %ld bytes for %s\n", size
, name
);
35 size
= round_to_pagesize(size
);
36 areaid
= create_area(name
, &virtadr
, B_ANY_KERNEL_ADDRESS
, size
,
37 B_CONTIGUOUS
, protection
);
39 ERROR("couldn't allocate area %s\n", name
);
42 rv
= get_memory_map(virtadr
, size
, &pe
, 1);
45 ERROR("couldn't get mapping for %s\n", name
);
52 TRACE("area = %" B_PRId32
", size = %ld, virt = %p, phy = %#" B_PRIxPHYSADDR
"\n",
53 areaid
, size
, virtadr
, pe
.address
);
59 map_mem(void **virt
, phys_addr_t phy
, size_t size
, uint32 protection
,
67 TRACE("mapping physical address %#" B_PRIxPHYSADDR
" with %" B_PRIuSIZE
68 " bytes for %s\n", phy
, size
, name
);
70 offset
= phy
& (B_PAGE_SIZE
- 1);
71 phyadr
= phy
- offset
;
72 size
= round_to_pagesize(size
+ offset
);
73 area
= map_physical_memory(name
, phyadr
, size
,
74 B_ANY_KERNEL_BLOCK_ADDRESS
, protection
, &mapadr
);
76 ERROR("mapping '%s' failed, error 0x%" B_PRIx32
" (%s)\n", name
,
77 area
, strerror(area
));
81 *virt
= (char *)mapadr
+ offset
;
83 TRACE("physical = %#" B_PRIxPHYSADDR
", virtual = %p, offset = %"
84 B_PRId32
", phyadr = %#" B_PRIxPHYSADDR
", mapadr = %p, size = %"
85 B_PRIuSIZE
", area = 0x%08" B_PRIx32
"\n", phy
, *virt
, offset
, phyadr
,
93 sg_memcpy(const physical_entry
*sgTable
, int sgCount
, const void *data
,
97 for (i
= 0; i
< sgCount
&& dataSize
> 0; i
++) {
98 size_t size
= min_c(dataSize
, sgTable
[i
].size
);
100 TRACE("sg_memcpy phyAddr %#" B_PRIxPHYSADDR
", size %lu\n",
101 sgTable
[i
].address
, size
);
103 vm_memcpy_to_physical(sgTable
[i
].address
, data
, size
, false);
105 data
= (char *)data
+ size
;
115 swap_words(void *data
, size_t size
)
117 uint16
*word
= (uint16
*)data
;
118 size_t count
= size
/ 2;
120 *word
= (*word
<< 8) | (*word
>> 8);