diff --git a/board/raspberrypi/rpi/rpi.c b/board/raspberrypi/rpi/rpi.c index d21750e2a0434d262a139681f805adc511abe847..6d7be11518ebf7131faac6f71386417b3ee7fcaa 100644 --- a/board/raspberrypi/rpi/rpi.c +++ b/board/raspberrypi/rpi/rpi.c @@ -10,6 +10,7 @@ #include <fdt_support.h> #include <fdt_simplefb.h> #include <lcd.h> +#include <memalign.h> #include <mmc.h> #include <asm/gpio.h> #include <asm/arch/mbox.h> diff --git a/board/samsung/common/misc.c b/board/samsung/common/misc.c index f0d69d4a48f0472e705f964b2a4522b31e879c95..e0e2c48632cf37907170f1888786a2f21361c5a1 100644 --- a/board/samsung/common/misc.c +++ b/board/samsung/common/misc.c @@ -12,6 +12,7 @@ #include <errno.h> #include <version.h> #include <malloc.h> +#include <memalign.h> #include <linux/sizes.h> #include <asm/arch/cpu.h> #include <asm/gpio.h> diff --git a/common/cmd_usb.c b/common/cmd_usb.c index 6874af7547148276f8b9564aa1e4b4e01ba8c7e1..6bdbbc5c05286e765abd1a901ef675499926d4ca 100644 --- a/common/cmd_usb.c +++ b/common/cmd_usb.c @@ -14,6 +14,7 @@ #include <common.h> #include <command.h> #include <dm.h> +#include <memalign.h> #include <asm/byteorder.h> #include <asm/unaligned.h> #include <part.h> diff --git a/common/env_fat.c b/common/env_fat.c index e4c848935ad19d7cac64d07102e7ebe39841b58b..d79d864a0c1216ee0e66a216b94d80a59e62de28 100644 --- a/common/env_fat.c +++ b/common/env_fat.c @@ -13,6 +13,7 @@ #include <environment.h> #include <linux/stddef.h> #include <malloc.h> +#include <memalign.h> #include <search.h> #include <errno.h> #include <fat.h> diff --git a/common/env_mmc.c b/common/env_mmc.c index 51e7707cb0f7b0bcc7ca9f377e4f42ca97255a9e..96398224cc849577a4150edf281337070e06d94b 100644 --- a/common/env_mmc.c +++ b/common/env_mmc.c @@ -12,6 +12,7 @@ #include <environment.h> #include <linux/stddef.h> #include <malloc.h> +#include <memalign.h> #include <mmc.h> #include <search.h> #include <errno.h> diff --git a/common/env_nand.c b/common/env_nand.c index 92e0e053dfe6b57abe49156bc246e780089a4af0..b32eeac9d7673c0137c3554dfd378f9ed2449bf4 100644 --- a/common/env_nand.c +++ b/common/env_nand.c @@ -19,6 +19,7 @@ #include <environment.h> #include <linux/stddef.h> #include <malloc.h> +#include <memalign.h> #include <nand.h> #include <search.h> #include <errno.h> diff --git a/common/env_ubi.c b/common/env_ubi.c index 77bbfa6ef43caf051ba5166dd8045944e6e7a821..e0dc5af8513e60d680080593a2a2fa91797964f0 100644 --- a/common/env_ubi.c +++ b/common/env_ubi.c @@ -11,6 +11,7 @@ #include <environment.h> #include <errno.h> #include <malloc.h> +#include <memalign.h> #include <search.h> #include <ubi_uboot.h> #undef crc32 diff --git a/common/usb.c b/common/usb.c index fbaf8ecbe232ce63170228727df1ed7fa23b060f..700bfc315b67f9f58276ccdcb8a88006ac71321f 100644 --- a/common/usb.c +++ b/common/usb.c @@ -29,6 +29,7 @@ #include <common.h> #include <command.h> #include <dm.h> +#include <memalign.h> #include <asm/processor.h> #include <linux/compiler.h> #include <linux/ctype.h> diff --git a/common/usb_hub.c b/common/usb_hub.c index 652a104361f63ee715a99e924ccd1145a2c66224..415b45c1f139451c685fd450a6477bde34859cab 100644 --- a/common/usb_hub.c +++ b/common/usb_hub.c @@ -26,6 +26,7 @@ #include <command.h> #include <dm.h> #include <errno.h> +#include <memalign.h> #include <asm/processor.h> #include <asm/unaligned.h> #include <linux/ctype.h> diff --git a/common/usb_kbd.c b/common/usb_kbd.c index 0227024441710041a4cbcb2fd5c30920ad9078a6..95912f99c767ff70265d33e99ce336845ba67a35 100644 --- a/common/usb_kbd.c +++ b/common/usb_kbd.c @@ -11,6 +11,7 @@ #include <dm.h> #include <errno.h> #include <malloc.h> +#include <memalign.h> #include <stdio_dev.h> #include <asm/byteorder.h> diff --git a/common/usb_storage.c b/common/usb_storage.c index b9784304086ecdc7ec7beaaeedb853af513824de..b390310ee35b554f3d152f39fbd1d9e77cc7132a 100644 --- a/common/usb_storage.c +++ b/common/usb_storage.c @@ -39,6 +39,7 @@ #include <errno.h> #include <inttypes.h> #include <mapmem.h> +#include <memalign.h> #include <asm/byteorder.h> #include <asm/processor.h> #include <dm/device-internal.h> diff --git a/disk/part_dos.c b/disk/part_dos.c index cf1a36ebb826699e9b391f6d68a9fad0c5806430..89263d38aaa03c609f4aa1ec3da83913832dee3d 100644 --- a/disk/part_dos.c +++ b/disk/part_dos.c @@ -17,6 +17,7 @@ #include <common.h> #include <command.h> #include <ide.h> +#include <memalign.h> #include "part_dos.h" #ifdef HAVE_BLOCK_DEVICE diff --git a/disk/part_efi.c b/disk/part_efi.c index 5856f9321118da1be38015d4bce89e8e498f1683..15627f29e8bd4ad33ba1688ad846d131e9c0cb8c 100644 --- a/disk/part_efi.c +++ b/disk/part_efi.c @@ -16,6 +16,7 @@ #include <ide.h> #include <inttypes.h> #include <malloc.h> +#include <memalign.h> #include <part_efi.h> #include <linux/ctype.h> diff --git a/disk/part_mac.c b/disk/part_mac.c index cbcb995c48c715b67ff4ab33826113c3f3d8daac..099e0a0035be07303ac6b69a646ab59ba6260819 100644 --- a/disk/part_mac.c +++ b/disk/part_mac.c @@ -15,6 +15,7 @@ #include <common.h> #include <command.h> +#include <memalign.h> #include <ide.h> #include "part_mac.h" diff --git a/drivers/block/ahci.c b/drivers/block/ahci.c index 0d19dd25a3441c95d1e159dfeacacdd4a2960cc3..82c68439f8630d0b6342676bf43910f33d5847b1 100644 --- a/drivers/block/ahci.c +++ b/drivers/block/ahci.c @@ -15,6 +15,7 @@ #include <asm/errno.h> #include <asm/io.h> #include <malloc.h> +#include <memalign.h> #include <scsi.h> #include <libata.h> #include <linux/ctype.h> diff --git a/drivers/mmc/dw_mmc.c b/drivers/mmc/dw_mmc.c index 1117fedefeb19a6fac62e8792db1b0c96136c4ff..a84c1e16d8b706332c86db4a439227d3601027cb 100644 --- a/drivers/mmc/dw_mmc.c +++ b/drivers/mmc/dw_mmc.c @@ -10,6 +10,7 @@ #include <common.h> #include <errno.h> #include <malloc.h> +#include <memalign.h> #include <mmc.h> #include <dwmmc.h> #include <asm-generic/errno.h> diff --git a/drivers/mmc/mmc.c b/drivers/mmc/mmc.c index 371c1ec21230ba34ae42c822369628b0810bb6c6..2a58702848b7f977bd3d10b12bd9138287fc1df5 100644 --- a/drivers/mmc/mmc.c +++ b/drivers/mmc/mmc.c @@ -16,6 +16,7 @@ #include <mmc.h> #include <part.h> #include <malloc.h> +#include <memalign.h> #include <linux/list.h> #include <div64.h> #include "mmc_private.h" diff --git a/drivers/mmc/rpmb.c b/drivers/mmc/rpmb.c index 9d0b8bc0c8f9b41fd91185729e87c3777e8c7a23..1c6888fc48f9370a2befa194743ea69d3be6926b 100644 --- a/drivers/mmc/rpmb.c +++ b/drivers/mmc/rpmb.c @@ -10,6 +10,7 @@ #include <config.h> #include <common.h> +#include <memalign.h> #include <mmc.h> #include <u-boot/sha256.h> #include "mmc_private.h" diff --git a/drivers/mtd/nand/tegra_nand.c b/drivers/mtd/nand/tegra_nand.c index debad4f791f97ee81c172b243f14d204a07b666d..a77db7b65dfa7d8519f555ba19845431c582c223 100644 --- a/drivers/mtd/nand/tegra_nand.c +++ b/drivers/mtd/nand/tegra_nand.c @@ -9,6 +9,7 @@ #include <common.h> #include <asm/io.h> +#include <memalign.h> #include <nand.h> #include <asm/arch/clock.h> #include <asm/arch/funcmux.h> diff --git a/drivers/net/e1000.c b/drivers/net/e1000.c index 7b830ff8c0515a66f763ff4c4270965f39a2e65a..2ba03ed73e45e40b177221cc47483465dba7a3cc 100644 --- a/drivers/net/e1000.c +++ b/drivers/net/e1000.c @@ -32,6 +32,7 @@ tested on both gig copper and gig fiber boards #include <common.h> #include <dm.h> #include <errno.h> +#include <memalign.h> #include <pci.h> #include "e1000.h" diff --git a/drivers/net/fec_mxc.c b/drivers/net/fec_mxc.c index bff5fd111971dc7e7c50f1ae3801d8b8eefe94ce..79f6737e8e9c9727c46591cfe543643c83b17d4e 100644 --- a/drivers/net/fec_mxc.c +++ b/drivers/net/fec_mxc.c @@ -10,6 +10,7 @@ #include <common.h> #include <malloc.h> +#include <memalign.h> #include <net.h> #include <netdev.h> #include <miiphy.h> diff --git a/drivers/net/rtl8169.c b/drivers/net/rtl8169.c index 7b6e20f30fbfe3ee4f0413be7dce4f4f645be219..ebd46b27e5fdeda6e2e543b2e652802d745cd1ec 100644 --- a/drivers/net/rtl8169.c +++ b/drivers/net/rtl8169.c @@ -44,6 +44,7 @@ #include <dm.h> #include <errno.h> #include <malloc.h> +#include <memalign.h> #include <net.h> #ifndef CONFIG_DM_ETH #include <netdev.h> diff --git a/drivers/spi/mxs_spi.c b/drivers/spi/mxs_spi.c index 2b9f395a97821568dc12c5e28feaaf42b42ffd50..627644b56bdf13f94fbdbae4d10f80cbbe06f0d1 100644 --- a/drivers/spi/mxs_spi.c +++ b/drivers/spi/mxs_spi.c @@ -12,6 +12,7 @@ #include <common.h> #include <malloc.h> +#include <memalign.h> #include <spi.h> #include <asm/errno.h> #include <asm/io.h> diff --git a/drivers/usb/eth/asix.c b/drivers/usb/eth/asix.c index 8a43e7c27d6fe263f1635206f30570ad92852ff8..ad083cf8ae4a28b4a6e56202f26dab0fcd56e592 100644 --- a/drivers/usb/eth/asix.c +++ b/drivers/usb/eth/asix.c @@ -10,6 +10,7 @@ #include <dm.h> #include <usb.h> #include <malloc.h> +#include <memalign.h> #include <linux/mii.h> #include "usb_ether.h" diff --git a/drivers/usb/eth/asix88179.c b/drivers/usb/eth/asix88179.c index 94dfe85eff3aa8b89aa5b85163e742f46e35539c..cf4085d76572c986c268ce06f3ab97e8b30dab9b 100644 --- a/drivers/usb/eth/asix88179.c +++ b/drivers/usb/eth/asix88179.c @@ -12,6 +12,7 @@ #include <linux/mii.h> #include "usb_ether.h" #include <malloc.h> +#include <memalign.h> #include <errno.h> /* ASIX AX88179 based USB 3.0 Ethernet Devices */ diff --git a/drivers/usb/eth/mcs7830.c b/drivers/usb/eth/mcs7830.c index c1b708600e891e553884ddbe4d91910ffe0be2b4..bbdad8b79ae49eace0e24acd36baa6928873b1ab 100644 --- a/drivers/usb/eth/mcs7830.c +++ b/drivers/usb/eth/mcs7830.c @@ -14,6 +14,7 @@ #include <errno.h> #include <linux/mii.h> #include <malloc.h> +#include <memalign.h> #include <usb.h> #include "usb_ether.h" diff --git a/drivers/usb/eth/smsc95xx.c b/drivers/usb/eth/smsc95xx.c index 1dcd088b8dfc4772ff2d621774aa04767fc13eff..dc8fa8891be6b95200d95ca696215bd04e36bdd8 100644 --- a/drivers/usb/eth/smsc95xx.c +++ b/drivers/usb/eth/smsc95xx.c @@ -11,6 +11,7 @@ #include <dm.h> #include <errno.h> #include <malloc.h> +#include <memalign.h> #include <usb.h> #include <asm/unaligned.h> #include <linux/mii.h> diff --git a/drivers/usb/gadget/ether.c b/drivers/usb/gadget/ether.c index 53f4672291f3d83a32cae43f4bd99caedba6c502..c5e35ee35077a37cca9a623c77c835dc75542bb2 100644 --- a/drivers/usb/gadget/ether.c +++ b/drivers/usb/gadget/ether.c @@ -17,6 +17,7 @@ #include <net.h> #include <usb.h> #include <malloc.h> +#include <memalign.h> #include <linux/ctype.h> #include "gadget_chips.h" diff --git a/drivers/usb/gadget/f_thor.c b/drivers/usb/gadget/f_thor.c index 06139ee04d2123f15d44a7613094e168962acb95..ff1481ba372304f3f8f5af5be18ea2185a6d57b2 100644 --- a/drivers/usb/gadget/f_thor.c +++ b/drivers/usb/gadget/f_thor.c @@ -18,6 +18,7 @@ #include <errno.h> #include <common.h> #include <malloc.h> +#include <memalign.h> #include <version.h> #include <linux/usb/ch9.h> #include <linux/usb/gadget.h> diff --git a/drivers/usb/host/dwc2.c b/drivers/usb/host/dwc2.c index 85236aeb2a48bc135248a46a12f4a9ae2d4c0a20..541c0f968701ef9ed6d9e290b54bd8e927b51fce 100644 --- a/drivers/usb/host/dwc2.c +++ b/drivers/usb/host/dwc2.c @@ -10,6 +10,7 @@ #include <errno.h> #include <usb.h> #include <malloc.h> +#include <memalign.h> #include <phys2bus.h> #include <usbroothubdes.h> #include <asm/io.h> diff --git a/drivers/usb/host/ehci-hcd.c b/drivers/usb/host/ehci-hcd.c index 88b670b7f8a76511fdc7c127e685b4c99b004270..135ef7262278c67f9c39f171560cd29b338963d1 100644 --- a/drivers/usb/host/ehci-hcd.c +++ b/drivers/usb/host/ehci-hcd.c @@ -15,6 +15,7 @@ #include <usb.h> #include <asm/io.h> #include <malloc.h> +#include <memalign.h> #include <watchdog.h> #include <linux/compiler.h> diff --git a/drivers/usb/host/ohci-hcd.c b/drivers/usb/host/ohci-hcd.c index 0ffd838db29e2535ba064f73b5add9973af8d240..9bde2b252c03783356a83ffbb6d1769e4fbdb11d 100644 --- a/drivers/usb/host/ohci-hcd.c +++ b/drivers/usb/host/ohci-hcd.c @@ -41,6 +41,7 @@ #endif #include <malloc.h> +#include <memalign.h> #include <usb.h> #include "ohci.h" diff --git a/drivers/usb/host/usb-uclass.c b/drivers/usb/host/usb-uclass.c index b17a7d762e03350fd01bb1225d59a5ce4f4a5701..c66ebb6678dee1d1340a7b9010fc4d96ae0c642c 100644 --- a/drivers/usb/host/usb-uclass.c +++ b/drivers/usb/host/usb-uclass.c @@ -10,6 +10,7 @@ #include <common.h> #include <dm.h> #include <errno.h> +#include <memalign.h> #include <usb.h> #include <dm/device-internal.h> #include <dm/lists.h> diff --git a/drivers/video/bcm2835.c b/drivers/video/bcm2835.c index 61d054dd8923d7423df19aa5ee13366c6e084d32..7867fe3895d00a763ff7b841554f46de1fd2ecd1 100644 --- a/drivers/video/bcm2835.c +++ b/drivers/video/bcm2835.c @@ -6,6 +6,7 @@ #include <common.h> #include <lcd.h> +#include <memalign.h> #include <asm/arch/mbox.h> #include <asm/global_data.h> diff --git a/fs/ext4/dev.c b/fs/ext4/dev.c index c77c02cdfce262cdb6910a4746b149f2b9f0d07f..20f52566f09c03bfeaa869855aa81649e109dc2c 100644 --- a/fs/ext4/dev.c +++ b/fs/ext4/dev.c @@ -25,6 +25,7 @@ #include <common.h> #include <config.h> +#include <memalign.h> #include <ext4fs.h> #include <ext_common.h> #include "ext4_common.h" diff --git a/fs/ext4/ext4_common.c b/fs/ext4/ext4_common.c index cab5465b9d4f9e99158dbdc8cd2f704e090c9b4b..c77e22d477a22576f484cb03b8fc72a6d0ef0556 100644 --- a/fs/ext4/ext4_common.c +++ b/fs/ext4/ext4_common.c @@ -24,6 +24,7 @@ #include <ext4fs.h> #include <inttypes.h> #include <malloc.h> +#include <memalign.h> #include <stddef.h> #include <linux/stat.h> #include <linux/time.h> diff --git a/fs/ext4/ext4_write.c b/fs/ext4/ext4_write.c index fbc4c4b1cc1a48e2f458a0ef30df97b451d40675..d346c065cd8a75e7f9dc3d1440b6aad951b50ebf 100644 --- a/fs/ext4/ext4_write.c +++ b/fs/ext4/ext4_write.c @@ -23,6 +23,7 @@ #include <common.h> +#include <memalign.h> #include <linux/stat.h> #include <div64.h> #include "ext4_common.h" diff --git a/fs/fat/fat.c b/fs/fat/fat.c index a863644d648068749a5803276d82ed5feca17604..06dcbb70057488b46b4a2d85afb08bbfeb7e2b4c 100644 --- a/fs/fat/fat.c +++ b/fs/fat/fat.c @@ -16,6 +16,7 @@ #include <asm/byteorder.h> #include <part.h> #include <malloc.h> +#include <memalign.h> #include <linux/compiler.h> #include <linux/ctype.h> diff --git a/include/common.h b/include/common.h index c48e5bc11bf5b7ffe1d6f4314a17aa431b247b9a..68b24d0778d4c2f64b2d608c14293387d130e5d3 100644 --- a/include/common.h +++ b/include/common.h @@ -973,93 +973,6 @@ int cpu_release(int nr, int argc, char * const argv[]); #define ROUND(a,b) (((a) + (b) - 1) & ~((b) - 1)) -/* - * ARCH_DMA_MINALIGN is defined in asm/cache.h for each architecture. It - * is used to align DMA buffers. - */ -#ifndef __ASSEMBLY__ -#include <asm/cache.h> -#endif - -/* - * The ALLOC_CACHE_ALIGN_BUFFER macro is used to allocate a buffer on the - * stack that meets the minimum architecture alignment requirements for DMA. - * Such a buffer is useful for DMA operations where flushing and invalidating - * the cache before and after a read and/or write operation is required for - * correct operations. - * - * When called the macro creates an array on the stack that is sized such - * that: - * - * 1) The beginning of the array can be advanced enough to be aligned. - * - * 2) The size of the aligned portion of the array is a multiple of the minimum - * architecture alignment required for DMA. - * - * 3) The aligned portion contains enough space for the original number of - * elements requested. - * - * The macro then creates a pointer to the aligned portion of this array and - * assigns to the pointer the address of the first element in the aligned - * portion of the array. - * - * Calling the macro as: - * - * ALLOC_CACHE_ALIGN_BUFFER(uint32_t, buffer, 1024); - * - * Will result in something similar to saying: - * - * uint32_t buffer[1024]; - * - * The following differences exist: - * - * 1) The resulting buffer is guaranteed to be aligned to the value of - * ARCH_DMA_MINALIGN. - * - * 2) The buffer variable created by the macro is a pointer to the specified - * type, and NOT an array of the specified type. This can be very important - * if you want the address of the buffer, which you probably do, to pass it - * to the DMA hardware. The value of &buffer is different in the two cases. - * In the macro case it will be the address of the pointer, not the address - * of the space reserved for the buffer. However, in the second case it - * would be the address of the buffer. So if you are replacing hard coded - * stack buffers with this macro you need to make sure you remove the & from - * the locations where you are taking the address of the buffer. - * - * Note that the size parameter is the number of array elements to allocate, - * not the number of bytes. - * - * This macro can not be used outside of function scope, or for the creation - * of a function scoped static buffer. It can not be used to create a cache - * line aligned global buffer. - */ -#define PAD_COUNT(s, pad) (((s) - 1) / (pad) + 1) -#define PAD_SIZE(s, pad) (PAD_COUNT(s, pad) * pad) -#define ALLOC_ALIGN_BUFFER_PAD(type, name, size, align, pad) \ - char __##name[ROUND(PAD_SIZE((size) * sizeof(type), pad), align) \ - + (align - 1)]; \ - \ - type *name = (type *) ALIGN((uintptr_t)__##name, align) -#define ALLOC_ALIGN_BUFFER(type, name, size, align) \ - ALLOC_ALIGN_BUFFER_PAD(type, name, size, align, 1) -#define ALLOC_CACHE_ALIGN_BUFFER_PAD(type, name, size, pad) \ - ALLOC_ALIGN_BUFFER_PAD(type, name, size, ARCH_DMA_MINALIGN, pad) -#define ALLOC_CACHE_ALIGN_BUFFER(type, name, size) \ - ALLOC_ALIGN_BUFFER(type, name, size, ARCH_DMA_MINALIGN) - -/* - * DEFINE_CACHE_ALIGN_BUFFER() is similar to ALLOC_CACHE_ALIGN_BUFFER, but it's - * purpose is to allow allocating aligned buffers outside of function scope. - * Usage of this macro shall be avoided or used with extreme care! - */ -#define DEFINE_ALIGN_BUFFER(type, name, size, align) \ - static char __##name[ALIGN(size * sizeof(type), align)] \ - __aligned(align); \ - \ - static type *name = (type *)__##name -#define DEFINE_CACHE_ALIGN_BUFFER(type, name, size) \ - DEFINE_ALIGN_BUFFER(type, name, size, ARCH_DMA_MINALIGN) - /* * check_member() - Check the offset of a structure member * diff --git a/include/memalign.h b/include/memalign.h index f78b9dd9dc7c31b16a0a97d97b9170bd27924f7a..a96003908397bda3e6b78c4ca08bc1872cbe5da3 100644 --- a/include/memalign.h +++ b/include/memalign.h @@ -13,9 +13,100 @@ */ #ifndef __ASSEMBLY__ #include <asm/cache.h> - #include <malloc.h> +/* + * The ALLOC_CACHE_ALIGN_BUFFER macro is used to allocate a buffer on the + * stack that meets the minimum architecture alignment requirements for DMA. + * Such a buffer is useful for DMA operations where flushing and invalidating + * the cache before and after a read and/or write operation is required for + * correct operations. + * + * When called the macro creates an array on the stack that is sized such + * that: + * + * 1) The beginning of the array can be advanced enough to be aligned. + * + * 2) The size of the aligned portion of the array is a multiple of the minimum + * architecture alignment required for DMA. + * + * 3) The aligned portion contains enough space for the original number of + * elements requested. + * + * The macro then creates a pointer to the aligned portion of this array and + * assigns to the pointer the address of the first element in the aligned + * portion of the array. + * + * Calling the macro as: + * + * ALLOC_CACHE_ALIGN_BUFFER(uint32_t, buffer, 1024); + * + * Will result in something similar to saying: + * + * uint32_t buffer[1024]; + * + * The following differences exist: + * + * 1) The resulting buffer is guaranteed to be aligned to the value of + * ARCH_DMA_MINALIGN. + * + * 2) The buffer variable created by the macro is a pointer to the specified + * type, and NOT an array of the specified type. This can be very important + * if you want the address of the buffer, which you probably do, to pass it + * to the DMA hardware. The value of &buffer is different in the two cases. + * In the macro case it will be the address of the pointer, not the address + * of the space reserved for the buffer. However, in the second case it + * would be the address of the buffer. So if you are replacing hard coded + * stack buffers with this macro you need to make sure you remove the & from + * the locations where you are taking the address of the buffer. + * + * Note that the size parameter is the number of array elements to allocate, + * not the number of bytes. + * + * This macro can not be used outside of function scope, or for the creation + * of a function scoped static buffer. It can not be used to create a cache + * line aligned global buffer. + */ +#define PAD_COUNT(s, pad) (((s) - 1) / (pad) + 1) +#define PAD_SIZE(s, pad) (PAD_COUNT(s, pad) * pad) +#define ALLOC_ALIGN_BUFFER_PAD(type, name, size, align, pad) \ + char __##name[ROUND(PAD_SIZE((size) * sizeof(type), pad), align) \ + + (align - 1)]; \ + \ + type *name = (type *)ALIGN((uintptr_t)__##name, align) +#define ALLOC_ALIGN_BUFFER(type, name, size, align) \ + ALLOC_ALIGN_BUFFER_PAD(type, name, size, align, 1) +#define ALLOC_CACHE_ALIGN_BUFFER_PAD(type, name, size, pad) \ + ALLOC_ALIGN_BUFFER_PAD(type, name, size, ARCH_DMA_MINALIGN, pad) +#define ALLOC_CACHE_ALIGN_BUFFER(type, name, size) \ + ALLOC_ALIGN_BUFFER(type, name, size, ARCH_DMA_MINALIGN) + +/* + * DEFINE_CACHE_ALIGN_BUFFER() is similar to ALLOC_CACHE_ALIGN_BUFFER, but it's + * purpose is to allow allocating aligned buffers outside of function scope. + * Usage of this macro shall be avoided or used with extreme care! + */ +#define DEFINE_ALIGN_BUFFER(type, name, size, align) \ + static char __##name[ALIGN(size * sizeof(type), align)] \ + __aligned(align); \ + \ + static type *name = (type *)__##name +#define DEFINE_CACHE_ALIGN_BUFFER(type, name, size) \ + DEFINE_ALIGN_BUFFER(type, name, size, ARCH_DMA_MINALIGN) + +/** + * malloc_cache_aligned() - allocate a memory region aligned to cache line size + * + * This allocates memory at a cache-line boundary. The amount allocated may + * be larger than requested as it is rounded up to the nearest multiple of the + * cache-line size. This ensured that subsequent cache operations on this + * memory (flush, invalidate) will not affect subsequently allocated regions. + * + * @size: Minimum number of bytes to allocate + * + * @return pointer to new memory region, or NULL if there is no more memory + * available. + */ static inline void *malloc_cache_aligned(size_t size) { return memalign(ARCH_DMA_MINALIGN, ALIGN(size, ARCH_DMA_MINALIGN));