forked from rsta2/circle
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CXHCIDevice::FreeSharedMem() implemented
This is a prerequisite for implementing the possibility to remove and reconnect USB devices. class CXHCISharedMemAllocator added
- Loading branch information
Showing
7 changed files
with
232 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// | ||
// xhcisharedmemallocator.h | ||
// | ||
// Circle - A C++ bare metal environment for Raspberry Pi | ||
// Copyright (C) 2020 R. Stange <rsta2@o2online.de> | ||
// | ||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
// | ||
#ifndef _circle_usb_xhcisharedmemallocator_h | ||
#define _circle_usb_xhcisharedmemallocator_h | ||
|
||
#include <circle/types.h> | ||
|
||
// Shared memory requirements of this xHCI driver: | ||
// | ||
// Size Align Boundary Count Object | ||
// ---- ----- -------- -------------- ----------------------------------------------- | ||
// 16 64 none 1 ERST | ||
// 248 64 4K 1 Scatchpad Buffer Array | ||
// 264 64 4K 1 DCBAA | ||
// 1024 64 4K <=32 Device Context | ||
// 1024 64 64K <=2+32*EPs TRB Ring (EPs = number of endpoints per device) | ||
// 124K 4K 4K 1 Scatchpad Buffers | ||
|
||
// We only maintain blocks with the following specification. Other blocks, which do not | ||
// fit these specification (blocks with smaller size/alignment/boundary requirements do | ||
// fit), can be allocated, but are lost, if they are freed (should not happen). | ||
#define XHCI_BLOCK_SIZE 1024 | ||
#define XHCI_BLOCK_ALIGN 64 | ||
#define XHCI_BLOCK_BOUNDARY 0x10000 | ||
|
||
struct TXHCIBlockHeader | ||
{ | ||
u32 nMagic; | ||
#define XHCI_BLOCK_MAGIC 0x58484349 | ||
u32 nSize; | ||
u32 nAlign; | ||
u32 nBoundary; | ||
TXHCIBlockHeader *pNext; | ||
u8 Data[0]; | ||
}; | ||
|
||
class CXHCISharedMemAllocator /// Shared memory allocation for the xHCI driver | ||
{ | ||
public: | ||
CXHCISharedMemAllocator (uintptr nMemStart, uintptr nMemEnd); | ||
~CXHCISharedMemAllocator (void); | ||
|
||
size_t GetFreeSpace (void) const; | ||
|
||
void *Allocate (size_t nSize, size_t nAlign, size_t nBoundary); | ||
|
||
void Free (void *pBlock); | ||
|
||
private: | ||
uintptr m_nMemStart; | ||
uintptr m_nMemEnd; | ||
|
||
TXHCIBlockHeader *m_pFreeList; | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
// | ||
// xhcisharedmemallocator.cpp | ||
// | ||
// Circle - A C++ bare metal environment for Raspberry Pi | ||
// Copyright (C) 2020 R. Stange <rsta2@o2online.de> | ||
// | ||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
// | ||
#include <circle/usb/xhcisharedmemallocator.h> | ||
#include <circle/logger.h> | ||
#include <assert.h> | ||
|
||
static const char From[] = "xhcialloc"; | ||
|
||
CXHCISharedMemAllocator::CXHCISharedMemAllocator (uintptr nMemStart, uintptr nMemEnd) | ||
: m_nMemStart (nMemStart), | ||
m_nMemEnd (nMemEnd), | ||
m_pFreeList (0) | ||
{ | ||
assert (m_nMemStart != 0); | ||
assert (m_nMemEnd > m_nMemStart); | ||
} | ||
|
||
CXHCISharedMemAllocator::~CXHCISharedMemAllocator (void) | ||
{ | ||
m_nMemStart = 0; | ||
m_nMemEnd = 0; | ||
|
||
m_pFreeList = 0; | ||
} | ||
|
||
size_t CXHCISharedMemAllocator::GetFreeSpace (void) const | ||
{ | ||
return m_nMemEnd > m_nMemStart ? m_nMemEnd - m_nMemStart : 0; | ||
} | ||
|
||
void *CXHCISharedMemAllocator::Allocate (size_t nSize, size_t nAlign, size_t nBoundary) | ||
{ | ||
assert (nSize > 0); | ||
assert (nAlign != 0); | ||
assert (nAlign <= nBoundary); | ||
assert (m_nMemStart != 0); | ||
assert (m_nMemEnd != 0); | ||
|
||
if ( nSize <= XHCI_BLOCK_SIZE | ||
&& nAlign <= XHCI_BLOCK_ALIGN | ||
&& nBoundary <= XHCI_BLOCK_BOUNDARY) | ||
{ | ||
if (m_pFreeList != 0) | ||
{ | ||
TXHCIBlockHeader *pBlockHeader = m_pFreeList; | ||
assert (pBlockHeader->nMagic == XHCI_BLOCK_MAGIC); | ||
assert (pBlockHeader->nSize == XHCI_BLOCK_SIZE); | ||
assert (pBlockHeader->nAlign == XHCI_BLOCK_ALIGN); | ||
assert (pBlockHeader->nBoundary == XHCI_BLOCK_BOUNDARY); | ||
|
||
m_pFreeList = pBlockHeader->pNext; | ||
pBlockHeader->pNext = 0; | ||
|
||
void *pResult = pBlockHeader->Data; | ||
assert (((uintptr) pResult & (XHCI_BLOCK_ALIGN-1)) == 0); | ||
|
||
return pResult; | ||
} | ||
|
||
nSize = XHCI_BLOCK_SIZE; | ||
nAlign = XHCI_BLOCK_ALIGN; | ||
nBoundary = XHCI_BLOCK_BOUNDARY; | ||
} | ||
|
||
m_nMemStart += sizeof (TXHCIBlockHeader); | ||
|
||
size_t nAlignMask = nAlign - 1; | ||
if (m_nMemStart & nAlignMask) | ||
{ | ||
m_nMemStart += nAlignMask; | ||
m_nMemStart &= ~nAlignMask; | ||
} | ||
|
||
size_t nBoundaryMask = nBoundary - 1; | ||
if ( (m_nMemStart & ~nBoundaryMask) | ||
!= ((m_nMemStart + nSize-1) & ~nBoundaryMask)) | ||
{ | ||
m_nMemStart += nBoundaryMask; | ||
m_nMemStart &= ~nBoundaryMask; | ||
} | ||
|
||
TXHCIBlockHeader *pBlockHeader = | ||
(TXHCIBlockHeader *) (m_nMemStart - sizeof (TXHCIBlockHeader)); | ||
|
||
m_nMemStart += nSize; | ||
|
||
if (m_nMemStart > m_nMemEnd) | ||
{ | ||
return 0; | ||
} | ||
|
||
pBlockHeader->nMagic = XHCI_BLOCK_MAGIC; | ||
pBlockHeader->nSize = (u32) nSize; | ||
pBlockHeader->nAlign = (u32) nAlign; | ||
pBlockHeader->nBoundary = (u32) nBoundary; | ||
pBlockHeader->pNext = 0; | ||
|
||
void *pResult = pBlockHeader->Data; | ||
assert (((uintptr) pResult & (XHCI_BLOCK_ALIGN-1)) == 0); | ||
|
||
return pResult; | ||
} | ||
|
||
void CXHCISharedMemAllocator::Free (void *pBlock) | ||
{ | ||
assert (pBlock != 0); | ||
|
||
TXHCIBlockHeader *pBlockHeader = | ||
(TXHCIBlockHeader *) ((uintptr) pBlock - sizeof (TXHCIBlockHeader)); | ||
assert (pBlockHeader->nMagic == XHCI_BLOCK_MAGIC); | ||
|
||
if ( pBlockHeader->nSize == XHCI_BLOCK_SIZE | ||
&& pBlockHeader->nAlign == XHCI_BLOCK_ALIGN | ||
&& pBlockHeader->nBoundary == XHCI_BLOCK_BOUNDARY) | ||
{ | ||
pBlockHeader->pNext = m_pFreeList; | ||
m_pFreeList = pBlockHeader; | ||
} | ||
else | ||
{ | ||
CLogger::Get ()->Write (From, LogWarning, | ||
"Trying to free shared memory at 0x%lX (size %u, align %u)", | ||
(uintptr) pBlock, pBlockHeader->nSize, pBlockHeader->nAlign); | ||
} | ||
} |