usb_mem.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /**
  2. * @file usb_mem.h
  3. * @brief
  4. *
  5. * Copyright (c) 2022 sakumisu
  6. *
  7. * Licensed to the Apache Software Foundation (ASF) under one or more
  8. * contributor license agreements. See the NOTICE file distributed with
  9. * this work for additional information regarding copyright ownership. The
  10. * ASF licenses this file to you under the Apache License, Version 2.0 (the
  11. * "License"); you may not use this file except in compliance with the
  12. * License. You may obtain a copy of the License at
  13. *
  14. * http://www.apache.org/licenses/LICENSE-2.0
  15. *
  16. * Unless required by applicable law or agreed to in writing, software
  17. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  18. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  19. * License for the specific language governing permissions and limitations
  20. * under the License.
  21. *
  22. */
  23. #ifndef _USB_MEM_H
  24. #define _USB_MEM_H
  25. #ifndef CONFIG_DCACHE_LINE_SIZE
  26. #define CONFIG_DCACHE_LINE_SIZE 32
  27. #endif
  28. #ifdef CONFIG_USB_DCACHE_ENABLE
  29. #define USB_NOCACHE_RAM_SECTION __attribute__((section(".nocache_ram")))
  30. #define USB_MEM_ALIGN32 __attribute__((aligned(CONFIG_DCACHE_LINE_SIZE)))
  31. #else
  32. #define USB_NOCACHE_RAM_SECTION
  33. #define USB_MEM_ALIGN32
  34. #endif
  35. #define usb_malloc(size) malloc(size)
  36. #define usb_free(ptr) free(ptr)
  37. #ifdef CONFIG_USB_DCACHE_ENABLE
  38. static inline void *usb_iomalloc(size_t size)
  39. {
  40. void *ptr;
  41. void *align_ptr;
  42. int uintptr_size;
  43. size_t align_size;
  44. uint32_t align = CONFIG_DCACHE_LINE_SIZE;
  45. /* sizeof pointer */
  46. uintptr_size = sizeof(void *);
  47. uintptr_size -= 1;
  48. /* align the alignment size to uintptr size byte */
  49. align = ((align + uintptr_size) & ~uintptr_size);
  50. /* get total aligned size */
  51. align_size = ((size + uintptr_size) & ~uintptr_size) + align;
  52. /* allocate memory block from heap */
  53. ptr = usb_malloc(align_size);
  54. if (ptr != NULL) {
  55. /* the allocated memory block is aligned */
  56. if (((unsigned long)ptr & (align - 1)) == 0) {
  57. align_ptr = (void *)((unsigned long)ptr + align);
  58. } else {
  59. align_ptr = (void *)(((unsigned long)ptr + (align - 1)) & ~(align - 1));
  60. }
  61. /* set the pointer before alignment pointer to the real pointer */
  62. *((unsigned long *)((unsigned long)align_ptr - sizeof(void *))) = (unsigned long)ptr;
  63. ptr = align_ptr;
  64. }
  65. return ptr;
  66. }
  67. static inline void usb_iofree(void *ptr)
  68. {
  69. void *real_ptr;
  70. real_ptr = (void *)*(unsigned long *)((unsigned long)ptr - sizeof(void *));
  71. usb_free(real_ptr);
  72. }
  73. #else
  74. #define usb_iomalloc(size) usb_malloc(size)
  75. #define usb_iofree(ptr) usb_free(ptr)
  76. #endif
  77. #endif