spu: mfc.lisp

the SPU's Memory Flow Controller: DMA and tag groups.

Other definitions

(defconstant +mfc-put+) source
(defconstant +mfc-put+ #x20)
(defconstant +mfc-putl+) source
(defconstant +mfc-putl+ #x24)
(defconstant +mfc-putr+) source
(defconstant +mfc-putr+ #x30)
(defconstant +mfc-putrl+) source
(defconstant +mfc-putrl+ #x34)
(defconstant +mfc-get+) source
(defconstant +mfc-get+ #x40)
(defconstant +mfc-getl+) source
(defconstant +mfc-getl+ #x44)
(defconstant +mfc-sndsig+) source
(defconstant +mfc-sndsig+ #xA0)
(defconstant +mfc-putlluc+) source
(defconstant +mfc-putlluc+ #xB0)
(defconstant +mfc-putllc+) source
(defconstant +mfc-putllc+ #xB4)
(defconstant +mfc-putqlluc+) source
(defconstant +mfc-putqlluc+ #xB8)
(defconstant +mfc-barrier+) source
(defconstant +mfc-barrier+ #xC0)
(defconstant +mfc-eieio+) source
(defconstant +mfc-eieio+ #xC8)
(defconstant +mfc-sync+) source
(defconstant +mfc-sync+ #xCC)
(defconstant +mfc-getllar+) source
(defconstant +mfc-getllar+ #xD0)
(defconstant +mfc-putllc-success+) source
(defconstant +mfc-putllc-success+ 0)
(defconstant +mfc-putllc-failure+) source
(defconstant +mfc-putllc-failure+ 1)
(defconstant +mfc-putlluc-success+) source
(defconstant +mfc-putlluc-success+ 2)
(defconstant +mfc-getllar-success+) source
(defconstant +mfc-getllar-success+ 4)
(defconstant +spu-event-tg+) source
(defconstant +spu-event-tg+ #x1)
(defconstant +spu-event-sn+) source
(defconstant +spu-event-sn+ #x2)
(defconstant +spu-event-mb+) source
(defconstant +spu-event-mb+ #x10)
(defconstant +spu-event-le+) source
(defconstant +spu-event-le+ #x80)
(defconstant +spu-event-s2+) source
(defconstant +spu-event-s2+ #x100)
(defconstant +spu-event-s1+) source
(defconstant +spu-event-s1+ #x200)
(defconstant +spu-event-lr+) source
(defconstant +spu-event-lr+ #x400)
(defun mfc-size-ok) source
(defun mfc-size-ok (lsa ea size)
  "a valid transfer: 1, 2, 4 or 8 bytes naturally aligned with the same
   offset in both quadwords, or a multiple of 16 up to 16 KiB, both aligned"
  (cond ((= size 0) t)
        ((or (= size 1) (= size 2) (= size 4) (= size 8))
         (and (= (logand lsa (1- size)) 0)
              (= (logand lsa 15) (logand ea 15))))
        (t (and (= (logand size 15) 0) (<= size 16384)
                (= (logand lsa 15) 0) (= (logand ea 15) 0)))))
(defun mfc-transfer) source
(defun mfc-transfer (cpu get lsa ea size)
  "copy SIZE bytes between LS address LSA and main memory at EA"
  (declare (type spu cpu) (type (unsigned-byte 32) lsa ea size))
  (dotimes (i size)
    (if get
        (setf (ls-byte (+ lsa i)) (mem-read 8 (+ ea i)))
        (mem-write 8 (+ ea i) (ls-byte (+ lsa i)))))
  nil)
(defun mfc-fail) source
(defun mfc-fail (cpu cmd)
  (setf (spu-mfc-error cpu) (logior #x100 cmd))  ; nonzero even for opcode 0
  (spu-halt cpu))
(defun mfc-list) source
(defun mfc-list (cpu get tag lsa list size)
  "a list command: SIZE/8 elements at LS address LIST, each a word with the
   stall-and-notify flag (bit 31) and the transfer size (bits 0..14) and a
   word with the EA; element n goes to LSA (advanced by each size rounded
   up to 16) with the low four bits of its EA"
  (let ((base (logand lsa +spu-qmask+)))
    (dotimes (n (floor size 8))
      (let* ((at (logand (+ list (* 8 n)) +spu-lslr+))
             (w0 (ls-read 32 at))
             (ea (ls-read 32 (+ at 4)))
             (esize (bit-field w0 0 15))
             (elsa (logior base (logand ea 15))))
        (unless (mfc-size-ok elsa ea esize)
          (mfc-fail cpu (if get +mfc-getl+ +mfc-putl+))
          (return-from mfc-list nil))
        (mfc-transfer cpu get elsa ea esize)
        (setq base (logand (+ base (logand (+ esize 15) (lognot 15))) +spu-lslr+))
        (when (logbitp 31 w0)
          ;; TODO: suspend the list until MFC_WrListStallAck
          (setf (spu-mfc-stall-stat cpu) (logior (spu-mfc-stall-stat cpu) (ash 1 tag))
                (spu-event-stat cpu) (logior (spu-event-stat cpu) +spu-event-sn+)))))
    nil))
(defun mfc-line-same) source
(defun mfc-line-same (cpu ea)
  "the 128-byte line at EA still holds the reservation snapshot"
  (dotimes (i 128 t)
    (unless (= (aref (spu-llr-data cpu) i) (mem-read 8 (+ ea i)))
      (return nil))))
(defun mfc-execute) source
(defun mfc-execute (cpu cmd tag size lsa ea)
  "perform one MFC command"
  (declare (type spu cpu) (type (unsigned-byte 8) cmd) (type (unsigned-byte 5) tag)
           (type (unsigned-byte 32) size lsa ea))
  (let ((base (logand cmd #xF4)))       ; without the b, f and s bits
    (cond
      ((or (= cmd +mfc-barrier+) (= cmd +mfc-eieio+) (= cmd +mfc-sync+)) nil)
      ((= cmd +mfc-getllar+)
       (let ((line (logand ea (lognot 127))) (to (logand lsa #x3FF80)))
         (dotimes (i 128)
           (let ((b (mem-read 8 (+ line i))))
             (setf (aref (spu-llr-data cpu) i) b
                   (ls-byte (+ to i)) b)))
         (setf (spu-llr-ea cpu) line (spu-llr-valid cpu) 1
               (spu-mfc-atomic-stat cpu) +mfc-getllar-success+ (spu-mfc-atomic-count cpu) 1)))
      ((= cmd +mfc-putllc+)
       (let ((line (logand ea (lognot 127))))
         (if (and (= (spu-llr-valid cpu) 1) (= (spu-llr-ea cpu) line) (mfc-line-same cpu line))
             (progn (mfc-transfer cpu nil (logand lsa #x3FF80) line 128)
                    (setf (spu-mfc-atomic-stat cpu) +mfc-putllc-success+))
             (setf (spu-mfc-atomic-stat cpu) +mfc-putllc-failure+))
         (setf (spu-llr-valid cpu) 0 (spu-mfc-atomic-count cpu) 1)))
      ((or (= cmd +mfc-putlluc+) (= cmd +mfc-putqlluc+))
       (let ((line (logand ea (lognot 127))))
         (mfc-transfer cpu nil (logand lsa #x3FF80) line 128)
         (when (= (spu-llr-ea cpu) line) (setf (spu-llr-valid cpu) 0))
         (when (= cmd +mfc-putlluc+)
           (setf (spu-mfc-atomic-stat cpu) +mfc-putlluc-success+ (spu-mfc-atomic-count cpu) 1))))
      ((or (= base +mfc-get+) (= base +mfc-put+) (= base +mfc-putr+))
       (if (mfc-size-ok lsa ea size)
           (mfc-transfer cpu (= base +mfc-get+) (logand lsa +spu-lslr+) ea size)
           (mfc-fail cpu cmd)))
      ((or (= base +mfc-getl+) (= base +mfc-putl+) (= base +mfc-putrl+))
       (if (and (= (logand size 7) 0) (<= size 16384))
           (mfc-list cpu (= base +mfc-getl+) tag lsa (logand ea #x3FFF8) size)
           (mfc-fail cpu cmd)))
      ((= base +mfc-sndsig+)
       ;; TODO: target another SPU's signal register; for now a 4-byte put
       (mfc-transfer cpu nil (logand lsa +spu-lslr+) ea 4))
      (t (mfc-fail cpu cmd)))
    nil))
(defun mfc-enqueue) source
(defun mfc-enqueue (cpu cmd)
  "queue CMD with the current parameters and mark its tag group busy"
  (declare (type spu cpu) (type (unsigned-byte 32) cmd))
  (let ((n (spu-mfc-qcount cpu)) (tag (bit-field (spu-mfc-tag cpu) 0 5)))
    (setf (aref (spu-mfc-qcmd cpu) n) (bit-field cmd 0 8)
          (aref (spu-mfc-qtag cpu) n) tag
          (aref (spu-mfc-qsize cpu) n) (spu-mfc-size cpu)
          (aref (spu-mfc-qlsa cpu) n) (spu-mfc-lsa cpu)
          (aref (spu-mfc-qea cpu) n) (logior (ash (spu-mfc-eah cpu) 32) (spu-mfc-eal cpu))
          (spu-mfc-qcount cpu) (1+ n)
          (spu-mfc-tag-busy cpu) (logior (spu-mfc-tag-busy cpu) (ash 1 tag))))
  nil)
(defun mfc-drain) source
(defun mfc-drain (cpu)
  "perform every queued command in order; their tag groups complete"
  (declare (type spu cpu))
  (dotimes (n (spu-mfc-qcount cpu))
    (mfc-execute cpu (aref (spu-mfc-qcmd cpu) n) (aref (spu-mfc-qtag cpu) n)
                 (aref (spu-mfc-qsize cpu) n) (aref (spu-mfc-qlsa cpu) n)
                 (wrap 32 (aref (spu-mfc-qea cpu) n))))
  (setf (spu-mfc-qcount cpu) 0
        (spu-mfc-tag-busy cpu) 0)
  (when (/= (spu-mfc-tag-mask cpu) 0)
    (setf (spu-event-stat cpu) (logior (spu-event-stat cpu) +spu-event-tg+)))
  nil)

Made from xspe at b2fac33349db by tools/gen-xspe.py. xspe is GPL-3.0-or-later.