cdrom: Align read buffers to 64 bytes
authorPaul Cercueil <paul@crapouillou.net>
Tue, 15 Apr 2025 11:18:05 +0000 (13:18 +0200)
committernotaz <notasas@gmail.com>
Tue, 22 Apr 2025 22:07:51 +0000 (01:07 +0300)
Align the address and size of read buffers to 64 bytes, which
should be bigger or equal than the size of the cache lines on most
systems.

This allows hardware CD-ROM implementations to perform reads using DMA,
which generally have tight requirements on alignment.

Signed-off-by: Paul Cercueil <paul@crapouillou.net>
libpcsxcore/cdrom-async.c
libpcsxcore/cdrom.c
libpcsxcore/cdrom.h

index 84395e2..c2b6450 100644 (file)
@@ -10,6 +10,7 @@
  *   GNU General Public License for more details.                          *
  ***************************************************************************/
 
+#include <stdalign.h>
 #include <stdlib.h>
 #include <string.h>
 #include "system.h"
@@ -104,12 +105,15 @@ static struct {
    u32 buf_cnt, thread_exit, do_prefetch, prefetch_failed, have_subchannel;
    u32 total_lba, prefetch_lba;
    int check_eject_delay;
-   u8 buf_local[CD_FRAMESIZE_RAW];  // single sector cache, not touched by the thread
+
+   // single sector cache, not touched by the thread
+   alignas(64) u8 buf_local[CD_FRAMESIZE_RAW_ALIGNED];
 } acdrom;
 
 static void lbacache_do(u32 lba)
 {
-   unsigned char msf[3], buf[CD_FRAMESIZE_RAW], buf_sub[SUB_FRAMESIZE];
+   alignas(64) unsigned char buf[CD_FRAMESIZE_RAW_ALIGNED];
+   unsigned char msf[3], buf_sub[SUB_FRAMESIZE];
    u32 i = lba % acdrom.buf_cnt;
    int ret;
 
index e52f74b..df958ba 100644 (file)
@@ -21,6 +21,7 @@
 * Handles all CD-ROM registers and functions.
 */
 
+#include <stdalign.h>
 #include <assert.h>
 #include "cdrom.h"
 #include "cdrom-async.h"
@@ -124,7 +125,7 @@ static struct {
        u8 AttenuatorLeftToLeftT, AttenuatorLeftToRightT;
        u8 AttenuatorRightToRightT, AttenuatorRightToLeftT;
 } cdr;
-static s16 read_buf[CD_FRAMESIZE_RAW/2];
+alignas(64) static s16 read_buf[CD_FRAMESIZE_RAW_ALIGNED / 2];
 
 struct SubQ {
        char res0[12];
index b8682b0..b762738 100644 (file)
@@ -41,6 +41,10 @@ extern "C" {
 #define CD_FRAMESIZE_RAW               2352
 #define DATA_SIZE                              (CD_FRAMESIZE_RAW - 12)
 
+/* CD_FRAMESIZE_RAW aligned to a cache line for DMA buffers
+ * (assuming a cache line of max. 64 bytes) */
+#define CD_FRAMESIZE_RAW_ALIGNED       2368
+
 #define SUB_FRAMESIZE                  96
 
 #define MSF2SECT(m, s, f)              (((m) * 60 + (s) - 2) * 75 + (f))